This question was
closed Feb 10, 2017 at 05:55 PM by
MickeyAnim for the following reason:
I found out myself
Question by
MickeyAnim · Feb 10, 2017 at 05:06 PM ·
playeraiwaitforsecondsenemyaisight
C# Enemy AI, when player is not visible i want the Enemy to chase the player for a few more seconds and then stop.
The enemy is sitting and if a player interacts with it it chases the player and if it cannot find the player in a few seconds the enemy will go sit again, the script works but i can't make the AI wait for a few seconds. any tips?
public class SittingEnemyAI : MonoBehaviour {
[Header("Transforms")]
public Transform ChairPoint;
public Transform player;
public Transform Front;
[Header("Sight Range")]
public float Range;
[HideInInspector]
public Animator anim;
[HideInInspector]
private NavMeshAgent agent;
[HideInInspector]
public bool Patrolling;
private bool lost;
void Awake ()
{
agent = GetComponent<NavMeshAgent>();
anim = GetComponentInChildren<Animator> ();
Patrolling = true;
}
void Update ()
{
if (Patrolling == true)
{
lost = false;
Lost ();
}
if (lost == true)
{
StartCoroutine (StillLooking ());
}
//Debug.DrawRay (Front.position, Front.forward * Range);
RaycastHit hit;
Ray View = new Ray (Front.position, Front.forward);
if (Physics.Raycast (View, out hit, Range))
{
if (hit.collider.tag == "Player")
{
Chasing ();
StopCoroutine (StillLooking ());
}
else
{
if (Patrolling == false)
{
lost = true;
}
}
}
else
{
if (Patrolling == false)
{
lost = true;
}
}
if (anim.GetCurrentAnimatorStateInfo (0).IsName ("sit_to_stand"))
{
anim.SetBool ("StandUp", false);
}
if (anim.GetCurrentAnimatorStateInfo (0).IsName ("Running"))
{
Chasing ();
}
if (anim.GetCurrentAnimatorStateInfo (0).IsName ("Walk"))
{
agent.destination = ChairPoint.position;
Patrolling = true;
}
if (anim.GetCurrentAnimatorStateInfo (0).IsName ("looking_around"))
{
agent.destination = this.transform.position;
anim.SetBool ("StandUp", false);
}
if (anim.GetCurrentAnimatorStateInfo (0).IsName ("Typing"))
{
transform.rotation = ChairPoint.transform.rotation;
}
}
public void Chasing()
{
anim.SetBool ("Chase", true);
agent.destination = player.position;
agent.speed = 3.0f;
agent.angularSpeed = 360.0f;
Vector3 lookAtPosition = player.position;
lookAtPosition.y = transform.position.y;
transform.LookAt (lookAtPosition);
anim.SetBool ("Sit", false);
Patrolling = false;
}
void Lost()
{
anim.SetBool ("Chase", false);
agent.speed = 0.5f;
Patrolling = true;
}
void OnTriggerEnter(Collider collider)
{
if (collider.tag == "EnemySit")
{
anim.SetBool ("Sit", true);
}
}
IEnumerator StillLooking()
{
Chasing ();
yield return new WaitForSeconds (3);
Lost ();
lost = false;
Patrolling = true;
}
}
Comment
Follow this Question
Related Questions
Trouble making jumping spider enemies 0 Answers
How to use IEnumerator? Pls help! 1 Answer
Help with enemy AI 1 Answer