I know this is a common question, but I’ve looked at so many other answers and none of them work for me. I have a character that randomly roams around the X and Z axis. I just can’t get it to face the direction that it’s moving.
Here’s the roam script:
public class Roam : MonoBehaviour {
public Vector3 newPos;
public int max;
public int min;
public float speed;
bool ready;
int waitTime;
void Start () {
newChangeDirection();
ready = true;
waitTime = Random.Range (1, 3);
}
void Update () {
transform.position = Vector3.MoveTowards (transform.position, newPos, speed * Time.deltaTime);
if ((transform.position == newPos) && ready)
{
StartCoroutine(wait());
}
}
void newChangeDirection()
{
int posX = Random.Range (min, max);
int posZ = Random.Range (min, max);
newPos = new Vector3 (posX, 0, posZ);
}
IEnumerator wait()
{
ready = false;
yield return new WaitForSeconds (waitTime);
newChangeDirection();
ready = true;
}
}
And here’s the code in the rotate script:
void Update () {
if (gameObject.tag == "npc")
{
transform.rotation = Quaternion.LookRotation (Vector3.forward, Vector3.up);
}
}
They’re both on the character object.
Any help would be greatly appreciated!