Search Unity

Moving an object waypoint by waypoint (GameObjects)

Discussion in 'Scripting' started by Danver, Aug 23, 2013.

  1. Danver

    Danver

    Joined:
    Aug 23, 2013
    Posts:
    3
    Hi everyone!
    I’m trying to make an object moving from waypoint to waypoint on a path. The Player can stop the object by pressing Space button. There will be slopes, climbs and moving platforms. And what I have:

    1. There is an object Path with children - waypoints (GameObjects). And also a script with array of waypoints.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class MyPath : MonoBehaviour {
    6.     public GameObject[] WayPoints;
    7.    
    8.     // Use this for initialization
    9.     void Start () {
    10.    
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.    
    16.     }
    17. }
    18.  
    2. There is Cube object with Collider, rigidBody and moving script. Use Gravity is turned off.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerControllerPath : MonoBehaviour {
    6.     public GameObject Path;  // link to Path object with waypoints-children
    7. PathMover pathScript; // link to script object of Path object
    8.     public GameObject targetVector; // current target-waypoint
    9.     public int wayIndex; // current item numb in waypoint array of Path object
    10.     public float speed; // speed
    11.    
    12.     // Use this for initialization
    13.     void Start () {
    14.         wayIndex = 0;
    15.         pathScript = Path.GetComponent<PathMover>();
    16.         targetVector = pathScript.WayPoints[wayIndex // getting new target-waypoint
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.         if(Input.GetKey(KeyCode.Space))  // if we press Space button        {
    22.             speed = 0;
    23.             this.rigidbody.drag = 20;
    24.         }
    25.         else  // if not, wet constant speed
    26. {
    27.             speed = 2f;
    28.             this.rigidbody.drag = 0;
    29.         }
    30.        
    31.         if(wayIndex <= pathScript.WayPoints.Length-1) // if we in range of array
    32.         {
    33.             if(Vector3.Distance(this.transform.position, targetVector.transform.position) < 1f)  // and we near the current target-waypoint
    34.             {  
    35.                 targetVector = pathScript.WayPoints[wayIndex]; // set new target-waypoint              
    36. wayIndex++; // increase array index of waypoints
    37.             }
    38.         }  
    39.  
    40.         this.transform.LookAt(targetVector.transform.position); // look at current target-waypoint     
    41. this.transform.position = Vector3.Slerp(this.transform.position, targetVector.transform.position, Time.deltaTime * speed); // move to the current target-waypoint
    42.     }
    43. }
    44.  
    Script was written in rush, but It works. When Cube object is getting close to target-waypoint, It’s getting slow. And I got this with every waypoint on the path. By idea, it should move with constant speed from waypoint to waypoint and without sharp corners.

    Thank you for any useful advice!
     
  2. Apprentice

    Apprentice

    Joined:
    Feb 9, 2012
    Posts:
    74
    this is how it actually works always: set target which is a vector3, then alway rotate towards the target and move your character along its transform.forward, while walking check if the minimaldistance is reached, if so then assign a new waypoint. your problem is that you change your rigidbody.drag and slowdown your character. You commented out the bracket in the first if line inside Update(), might be not that good either. I always do the rotations with q1 = Quaternion.Lookrotation(transform.position-targetposition) and then transform.rotation = Quaternion.Slerp(transform.rotation, q1,timestuff); just try it out.
     
    Thatworkspace likes this.
  3. Danver

    Danver

    Joined:
    Aug 23, 2013
    Posts:
    3
    Apprentice, good solution about rotation!

    This code works better. I use constant Cube's positon for moving. But when I switch on Gravity in rigidBody... Cube's behavior becomes freaky. Don't understand why it does...

    Code (csharp):
    1.  
    2.     public GameObject Path;
    3.     PathMover pathScript;
    4.     public GameObject targetVector;
    5.     public int wayIndex;
    6.     public float speed;
    7.     private Vector3 oldPos;
    8.    
    9.     // Use this for initialization
    10.     void Start () {
    11.         oldPos = this.transform.position;
    12.         wayIndex = 0;
    13.         pathScript = Path.GetComponent<PathMover>();
    14.         targetVector = pathScript.WayPoints[wayIndex];
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update () {
    19.         if(Input.GetKey(KeyCode.Space))
    20.         {
    21.             speed = 0;
    22.             this.rigidbody.drag = 20;
    23.         }
    24.         else
    25.         {
    26.             speed = 0.5f;
    27.             this.rigidbody.drag = 0;
    28.         }
    29.        
    30.         if(wayIndex <= pathScript.WayPoints.Length-1)
    31.         {
    32.             if(Vector3.Distance(this.transform.position, targetVector.transform.position) < 1f)
    33.             {  
    34.                 targetVector = pathScript.WayPoints[wayIndex];
    35.                 wayIndex++;
    36.                 oldPos = this.transform.position;
    37.             }
    38.         }
    39.             var q1 = Quaternion.LookRotation(targetVector.transform.position - this.transform.position);
    40.  
    41.             this.transform.rotation = Quaternion.Slerp(this.transform.rotation, q1,Time.deltaTime);
    42.  
    43.             this.transform.position += ((targetVector.transform.position - oldPos) * Time.deltaTime * speed);
    44.     }
    45. }
    46.  
     
    Last edited: Aug 23, 2013
  4. Danver

    Danver

    Joined:
    Aug 23, 2013
    Posts:
    3
    Yesterday I found one bug... When the distance between two waypoint is too large, Cube starts circling and goes away from waypoint... Have you any idea?
     
  5. Apprentice

    Apprentice

    Joined:
    Feb 9, 2012
    Posts:
    74
    this might be the problem... this.transform.position += ((targetVector.transform.position - oldPos) * Time.deltaTime * speed);...
    why dont you just say transform.Translate(transform.forward*speed*Time.deltaTime); But because you use rigidbody you might want to freeze rotation for X and Z and use rigidbody.AddForce() or rigidbody.velocity to mvoe your character and when you use rigidbody your code should be inside of FixedUpdate().

    Edit: if you have a bug use Debug.Log() to check the values that you think might be not correct.