Help with enemy chase script 2D C#
Hi guys!I made a script "enemy patrol" that works perfectly fine but when i add in the same script the script to chase the player if he gets too close it starts to freak out.Here is the code (c# only):
using UnityEngine; using System.Collections;
public class EnemyAITest : MonoBehaviour {
public Transform[] patrolpoints;
int currentPoint;
public float speed=0.5f;
public float timestill=2f;
public float sight=3f;
Animator anim;
public float force;
public GameObject player;
public Enemy enemy;
[SerializeField]
private BoxCollider2D range;
public float chaseDistance = 0.5f;
public float curDistance;
public bool playerInRange;
[SerializeField]
public Chase chase;
[SerializeField]
public EnemyAI patrol;
public Transform targetEnemy;
public Transform targetPlayer;
public BoxCollider2D playerCollider;
public bool isChased;
//public Transform target;
// Use this for initialization
void Start () {
player = GameObject.FindGameObjectWithTag ("Player");
playerInRange = false;
anim = GetComponent<Animator> ();
StartCoroutine ("Patrol");
anim.SetBool("walking",true);
Physics2D.queriesStartInColliders = false;
}
// Update is called once per frame
void Update () {
curDistance = Vector3.Distance (targetPlayer.position, targetEnemy.position);
if (playerCollider.IsTouching(range))
{
playerInRange = true;
}
else
{
playerInRange = false;
}
if ((playerInRange) && (curDistance <= chaseDistance))
{
StopCoroutine ("Patrol");
isChased = true;
ChaseCheck (speed);
//targetEnemy.position += (targetPlayer.position - targetEnemy.position).normalized * speed * Time.deltaTime;
}
if((!playerInRange) && ((targetPlayer.position.x <= (patrolpoints[0].position.x -1)) || (targetPlayer.position.x >= (patrolpoints[1].position.x + 1))))
{
StartCoroutine ("Patrol");
isChased = false;
}
RaycastHit2D hit= Physics2D.Raycast (transform.position, transform.localScale.x * Vector2.right, sight);
if (hit.collider != null && hit.collider.tag == "Player")
GetComponent<Rigidbody2D> ().AddForce (Vector3.up*force + (hit.collider.transform.position-transform.position)*force);
}
public IEnumerator Patrol()
{
while (true) {
if(transform.position.x== patrolpoints[currentPoint].position.x )
{
currentPoint++;
anim.SetBool("walking",false);
yield return new WaitForSeconds(timestill);
anim.SetBool("walking",true);
}
if(currentPoint >=patrolpoints.Length)
{
currentPoint=0;
}
transform.position=Vector2.MoveTowards(transform.position,new Vector2(patrolpoints[currentPoint].position.x,transform.position.y),speed);
if(transform.position.x> patrolpoints[currentPoint].position.x)
transform.localScale=new Vector3(-1,1,1);
else if (transform.position.x< patrolpoints[currentPoint].position.x)
transform.localScale= Vector3.one;
yield return null;
}
}
public void ChaseCheck (float speed)
{
targetEnemy.position += (targetPlayer.position - targetEnemy.position).normalized * speed * 1;
//targetEnemy.position = Vector2.MoveTowards (targetEnemy.position, new Vector2 (targetPlayer.position.x, targetEnemy.position.y), speed * Time.deltaTime);
//targetEnemy.Translate(Vector2.MoveTowards(targetEnemy.position, targetPlayer.position,chaseDistance) * speed * Time.deltaTime);
}
}
Thx for your help :3.
Answer by ArturoSR · Sep 11, 2016 at 12:47 PM
@CristiBala24 Hello there.
OK, is possible that your issue is in the section where you check the foe distance, try to change it to only this:
// Instead use it like this
if (playerInRange && curDistance <= chaseDistance) {
// Here goes your code
} else if (curDistance > chaseDistance) {
// Here, change the playerInRange to false and put the rest of your code.
}
So, this way you can simplify a little your code and recommend to you to remove the else where you check if it's not touching the player, normally you only campare the first option and when the situation changes, that reset the behavior, it's something like: "I see you, going behind you", but if he is too faraway: "Mmmh!, better return to my patrolling", something like that, cheers.
@$$anonymous$$oSR This way the patrol keeps resetting while the player is out of chase range. Hope you can help.Thanks!
@$$anonymous$$oSR thanks for your help but we solved our problem! :)