Im trying to get my enemy AI 2D to follow me, my code is at the bottom. when i run the script it moves left, but if i jump over the enemy he stops and wont proceed till the player is to the enemy’s left again.
not my code btw.
using UnityEngine;
using System.Collections;
public class newai : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int maxDistance;
private Transform myTransform;
void Awake() {
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
maxDistance = 2;
}
// Update is called once per frame
void FixedUpdate () {
Debug.DrawLine(target.position, myTransform.position, Color.yellow);
//Look at target
//Move towards target
myTransform.position -= myTransform.right * moveSpeed * Time.deltaTime;
if (target.position.x < myTransform.position.x) myTransform.position -= myTransform.right * moveSpeed * Time.deltaTime; // player is left of enemy, move left
else if (target.position.x > myTransform.position.x) myTransform.position += myTransform.right * moveSpeed * Time.deltaTime; // player is right of enemy, move right
}
}