Hi trying to move a navmeshagent to a desti9nation but he either does not move at all or goes off in some very odd direction stopping at the same spot as if he has reached his destination. Code (CSharp): using UnityEngine; using System.Collections; public class randomwalk : MonoBehaviour { private Transform myTransform; private NavMeshAgent agent; bool sight=true; bool ready=true; int randy = 0; float Addd = 0f; float Addd2 = 0f; Vector3 Ad = new Vector3 (0, 0, 0); void Awake() { myTransform = transform; } void Start() { agent = GetComponent<NavMeshAgent> (); } // Update is called once per frame void Update () { if (sight == true) { randy = Random.Range (-50, 50);// random number is always the same not exactly random. sight = false; } if (ready == true) { Addd = myTransform.position.x + randy; Addd2=myTransform.position.z + randy; Ad = new Vector3 (Addd, 0, Addd2);//if i pass in any numbers here he doesn't move at all!! agent.SetDestination (Ad);Debug.Log (Ad);//does not go to this destination at all ready = false;agent.speed=7; } if ((myTransform.position.x - Ad.x < 2) && (myTransform.position.x - Ad.x > -2) && (myTransform.position.z - Ad.z < 2) && (myTransform.position.z - Ad.z > -2)) { sight = true; } } }
Well tried it in a blank project and it worked fine but not in my main project. And the random is not random at all in the project always is exactly the same number any idea what what cause that no idea how to debug this.
void Awake() { myTransform = transform; } I think it's because of that? Try putting it in the update function instead?
Bah just saw what it is my y value is set to 0 on my test area but the terrain in my main area is set at 85 bloody annoying sorry to waste anyones time who read this just had to set y height appropriately.