I am making a game while I learn and I have a simple script where AI “zombies” spawn and make their way towards me. Everything is working just how it should so far except as they get close to me they begin to tilt backwards until they are flat once they reach me. This is the code I am using for the AI. Can anyone tell me why they are tilting backwards because I am stumped so far.
public Transform target;
public float moveSpeed;
public float rotationSpeed;
private float distance;
private Transform myTransform;
void Awake ()
{
myTransform = transform;
}
// Use this for initialization
void Start ()
{
GameObject follow = GameObject.FindGameObjectWithTag ("Player");
target = follow.transform;
distance = 1.5f;
}
// Update is called once per frame
void Update ()
{
Debug.DrawLine (target.position, myTransform.position);
//Look at target
myTransform.rotation = Quaternion.Slerp (myTransform.rotation, Quaternion.LookRotation (target.position - myTransform.position), rotationSpeed * Time.deltaTime);
if (Vector3.Distance (target.position, myTransform.position) > distance)
{
//Move towards target
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}