Skip to content

Instantly share code, notes, and snippets.

@iendsl
Created March 27, 2015 00:08
Show Gist options
  • Save iendsl/553170407de3c4ffeeeb to your computer and use it in GitHub Desktop.
Save iendsl/553170407de3c4ffeeeb to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
public class DogWalkingBehavior : MonoBehaviour {
#region properties
delegate void DogBehavior();
DogBehavior currentDogBehavior;
public GameObject[] followSpots;
public GameObject centralHouseLocation;
public GameObject nearPlayer;
public GameObject followObject;
public float fastSpeed;
public float slowSpeed;
float slowingSpeed;
public float gravity;
public float moveTowardHouseDistance = 10;
public float dogMoveTowardHouse;
public float timeBetweenMovementChecks = 1;
public float timeBetweenSpotChecks = 1;
public GUISkin skin;
public AudioClip[] barks;
bool recentPlayerMovement;
int playerStillCount;
GameObject player;
CharacterController controller;
GameObject leash;
public bool cororunning;
public bool newSpotCoro;
[HideInInspector]
public bool leftArea;
bool leftAreaRan;
private Vector3 moveDirection = Vector3.zero;
GameObject nextSpot;
public string characterName;
public string sceneName;
int currentLine = 0;
string[] lines;
PartyGuests thisGuest;
float checkDelayIndex =0;
#endregion
#region unityfunctions
void Start () {
player = GameObject.FindGameObjectWithTag ("Player");
nextSpot = followSpots [0];
followObject.transform.position = followSpots [0].transform.position;
controller = GetComponent<CharacterController> ();
currentDogBehavior = WalkingAround;
slowingSpeed = fastSpeed;
thisGuest = GuestList.guestlist.characters [characterName];
lines = thisGuest.scenes [sceneName].dialog;
leash = GameObject.FindGameObjectWithTag ("Leash");
leash.SetActive (false);
}
void Update () {
//Debug.DrawRay (player.transform.position, (centralHouseLocation.transform.position-player.transform.position) *100, Color.red);
if (!leftArea) {
currentDogBehavior = WalkingAround;
} else {
if(!leftAreaRan){
currentDogBehavior = LeftArea;
}else{
currentDogBehavior = LeftAreaInput;
}
}
FollowObjectBehavior ();
currentDogBehavior ();
}
void OnGUI(){
if (leftArea) {
GUI.skin = skin;
GUI.Label (new Rect(0,0,Screen.width,Screen.height),lines [currentLine]);
}
}
#endregion
void WalkingAround(){
checkDelayIndex += 1 * Time.deltaTime;
if (checkDelayIndex > timeBetweenSpotChecks) {
newSpotCoro = false;
checkDelayIndex = 0;
}
moveDirection = Vector3.zero;
if (!newSpotCoro) {
ChooseANewSpot();
}
if (controller.isGrounded) {
Vector3 target = followObject.transform.position;
target.y = transform.position.y;
moveDirection = target - transform.position;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move (moveDirection.normalized * fastSpeed * Time.deltaTime);
slowingSpeed = fastSpeed;
}
void StandingStill(){
Debug.Log ("standingStill");
slowingSpeed = Mathf.Lerp (slowingSpeed, slowSpeed, Time.deltaTime);
if (!newSpotCoro) {
if(playerStillCount > 2){
nextSpot = nearPlayer;
}else{
int index = Random.Range(0,followSpots.Length-1);
nextSpot = followSpots[index];
ChooseANewSpot();
}
}
if (controller.isGrounded) {
moveDirection = Vector3.MoveTowards(transform.position,nextSpot.transform.position,1);
moveDirection *= slowingSpeed;
}
moveDirection.y -= gravity * Time.deltaTime;
controller.Move (moveDirection * Time.deltaTime);
}
void LeftArea(){
int randomBark = Random.Range (0, barks.Length);
audio.PlayOneShot (barks [randomBark]);
player.transform.position = player.transform.position + Vector3.up * 5;
Ray towardHouse = new Ray (player.transform.position, centralHouseLocation.transform.position - player.transform.position);
Transform lookTowardHouse;
SceneControl.control.inScene = true;
SceneControl.control.keepCamMoving = true;
currentLine = RandomLine ();
lookTowardHouse = player.transform;
player.transform.position = towardHouse.GetPoint (moveTowardHouseDistance);
lookTowardHouse.LookAt (centralHouseLocation.transform);
//all one line to set the players rotation to make sure the player is looking toward the house and the dog
player.transform.eulerAngles = new Vector3 (0, lookTowardHouse.eulerAngles.y, 0);
//end the players rotation setting
transform.position = towardHouse.GetPoint (moveTowardHouseDistance + dogMoveTowardHouse);
leftAreaRan = true;
}
void LeftAreaInput(){
JustGravity ();
if (controller.isGrounded && Input.GetKeyDown (KeyCode.Space)) {
SceneControl.control.inScene = false;
SceneControl.control.keepCamMoving = false;
leftArea = false;
leftAreaRan = false;
}
}
void JustGravity(){
Vector3 moveVector = new Vector3 ();
moveVector.y -= gravity;
controller.Move (moveVector * Time.deltaTime);
}
void FollowObjectBehavior(){
followObject.transform.position = Vector3.Lerp (followObject.transform.position, nextSpot.transform.position, Time.deltaTime * slowSpeed);
}
int RandomLine(){
int newLine;
newLine = Random.Range (0, lines.Length - 1);
while(newLine == currentLine) {
newLine = Random.Range(0, lines.Length -1);
}
return newLine;
}
#region ienumerators
void ChooseANewSpot(){
newSpotCoro = true;
int index = Random.Range(0,followSpots.Length);
nextSpot = followSpots[index];
//Debug.Log (index);
}
#endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment