Search Unity

Random Waypoint script. Please help!!

Discussion in 'Scripting' started by mcgamerx19, Aug 20, 2014.

  1. mcgamerx19

    mcgamerx19

    Joined:
    Jul 23, 2014
    Posts:
    19
    So I'm looking to make my model wander around once at a certain speed in to a random waypoint.
    I was thinking. And thank you if you reply!! I've been looking for a solution online and haven't found one.

    Code (JavaScript):
    1.  
    2. function Start ()
    3. {
    4.     InvokeRepeating("Tick", 300, 300); //Every 3mins run "tick"
    5. }
    6.  
    7. function Tick ()
    8. {
    9.     //waypoint to a random location. Moving position at certain speed
    10. }
     
    Last edited: Aug 20, 2014
  2. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    Posting code: http://forum.unity3d.com/threads/using-code-tags-properly.143875/
    Cleaner approach would be to invoke your movement code in FixedUpdate method: http://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html
    Assuming your object has a Rigidbody component you should move it around by applying force to it: http://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

    EDIT: For getting random values for your movement checkout Random class: http://docs.unity3d.com/ScriptReference/Random.html
     
    Last edited: Aug 20, 2014
  3. mcgamerx19

    mcgamerx19

    Joined:
    Jul 23, 2014
    Posts:
    19
    Tried everything and nothing seems to work.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Make a blank scene, drop a cube in so you can see it in the camera, and add this little script to the cube itself.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RunToRandomPoint : MonoBehaviour
    5. {
    6.     Vector3 targetPos;
    7.     float MovementSpeed = 5.0f;
    8.     float CloseEnough = 0.5f;
    9.  
    10.     IEnumerator Start ()
    11.     {
    12.         targetPos = transform.position;
    13.         while(true)
    14.         {
    15.             // do not move
    16.             yield return new WaitForSeconds( 2.0f);
    17.             // choose new location
    18.             targetPos = new Vector3( Random.Range ( -10, 10), Random.Range( -5, 5));
    19.             // wait until we arrive
    20.             while( (transform.position - targetPos).magnitude > CloseEnough)
    21.             {
    22.                 yield return new WaitForSeconds(0.0f);
    23.             }
    24.         }
    25.     }
    26.  
    27.     void Update ()
    28.     {
    29.         // have we arrived?
    30.         if ((transform.position - targetPos).magnitude > CloseEnough)
    31.         {
    32.             // nope, turn to and go
    33.             transform.LookAt( targetPos);
    34.             transform.position += (targetPos - transform.position).normalized *
    35.                 MovementSpeed * Time.deltaTime;
    36.         }
    37.     }
    38. }
    39.