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.