Search Unity

just me been an idiot (sorted)

Discussion in 'Scripting' started by tawdry, Jan 31, 2015.

  1. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    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):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class randomwalk : MonoBehaviour {
    5.     private Transform myTransform;
    6.     private NavMeshAgent agent;
    7.     bool sight=true;
    8.     bool ready=true;
    9.     int randy = 0;
    10.     float Addd = 0f;
    11.     float Addd2 = 0f;
    12.     Vector3 Ad = new Vector3 (0, 0, 0);
    13.  
    14.     void Awake() {
    15.         myTransform = transform;
    16.             }
    17.     void Start() {
    18.  
    19.         agent = GetComponent<NavMeshAgent> ();
    20.  
    21.     }
    22.  
    23.  
    24.     // Update is called once per frame
    25.     void Update ()    {  
    26.  
    27.  
    28.         if (sight == true)
    29.         {    randy = Random.Range (-50, 50);// random number is always the same not exactly random.
    30.             sight = false;
    31.                      
    32.         }
    33.              
    34.         if (ready == true) {  
    35.             Addd = myTransform.position.x + randy;
    36.             Addd2=myTransform.position.z + randy;
    37.             Ad = new Vector3 (Addd, 0, Addd2);//if i pass in any numbers here he doesn't move at all!!
    38.             agent.SetDestination (Ad);Debug.Log (Ad);//does not go to this destination at all
    39.             ready = false;agent.speed=7;
    40.             }
    41.      
    42.      
    43.  
    44.         if ((myTransform.position.x - Ad.x < 2) && (myTransform.position.x - Ad.x > -2) &&
    45.             (myTransform.position.z - Ad.z < 2) && (myTransform.position.z - Ad.z > -2)) {
    46.             sight = true;
    47.         }
    48.      
    49.     }
    50. }
     
  2. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    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.
     
  3. RaulG

    RaulG

    Joined:
    May 31, 2013
    Posts:
    74
    1. void Awake() {
    2. myTransform = transform;
    3. }
    I think it's because of that? Try putting it in the update function instead?
     
  4. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    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.