I am following a tutorial by BergZergArcade [1]
and I have definitely copied out the script correctly, but instead of using a cube, I am using an NPC model. The NPC turns towards me, but if it gets too close, it rotates so that is is leaning backwards, and also, it lifts off the ground too! I have made it a rigidbody with no luck, because it still flies and rotates awkwardly. Here is the code BergZergArcade used:
using UnityEngine;
using System.Collections;
public class EnemyAi : MonoBehaviour {
public Transform target;
public int moveSpeed;
public int rotationSpeed;
private Transform myTransform;
void Awake()
{
myTransform = transform;
}
// Use this for initialization
void Start () {
GameObject go = GameObject.FindGameObjectWithTag("Player");
target = go.transform;
}
// Update is called once per frame
void Update ()
{
Debug.DrawLine(target.position, myTransform.position, Color.clear);
//Look At Target
myTransform.rotation = Quaternion.Slerp(myTransform.rotation, Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed * Time.deltaTime);
//Move Towards Target
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
}
I considered posting on the video, but the last comment was posted a month ago, and that’s too long for me too wait.
[1]: 4. Unity3d Tutorial - Enemy AI 2/2 - YouTube