Total newbie at scripting here.
When this object hits a tagged wall, it needs to turn around and run for a few seconds then go back to it’s previous state. It’s doing everything correct except running straight for a few seconds. Its hitting the wall then it rotates but it just sits there for a few seconds instead of running in a straight line, then it goes back to it’s previous state of fleeing the player like it’s supposed to.
using UnityEngine;
using System.Collections;
public class catScript : MonoBehaviour {
public Transform target;
public Transform wall01;
public Transform wall02;
public Transform wall03;
public Transform wall04;
public float catSpeed;
public float catSpeed2;
public float stareAtPlayerDist;
public float fleePlayerDist;
public bool hitWallRun = false;
// Use this for initialization
void Start () {
}
void OnTriggerEnter(Collider other)
{
if (other.tag.Equals ("catZone")) {
Debug.Log ("I hit a wall");
hitWallRun = true;
Debug.Log ("cat hit boundry");
transform.Rotate (0, 60, 0);
StartCoroutine ("turnBoolFalse");
}
}
// Update is called once per frame
void Update () {
//z is forward, y is up
//Distance between object and player
float dist = Vector3.Distance (target.position, transform.position);
if (dist <= stareAtPlayerDist && dist > fleePlayerDist && hitWallRun == false)
{
transform.LookAt (target);
}
else if (dist <= fleePlayerDist && hitWallRun == false)
{
CharacterController controller = GetComponent<CharacterController> ();
Vector3 forward = transform.TransformDirection (Vector3.forward);
transform.LookAt (target);
transform.Rotate(0,180,0);
controller.SimpleMove (forward * catSpeed);
}
}
IEnumerator turnBoolFalse()
{
do {
CharacterController controller = GetComponent<CharacterController> ();
Vector3 forward = transform.TransformDirection (Vector3.forward);
controller.SimpleMove (forward * catSpeed2);
yield return new WaitForSeconds (5.0f);
hitWallRun = false;
} while (hitWallRun == false);
}
}