Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Simple Waypoint Rotation?

Discussion in 'Scripting' started by unitynoob24, Feb 8, 2016.

  1. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398
    Hey guys!

    I just finished setting up a really basic waypoint system for a basic cube. It moves around an array of transforms and works great. What I would like to do now is rotate the front facing with the direction that it is moving, as the transforms are kind of in a circle, I would like it to appear as if this cube is spinning towards the direction of the next way point.

    This is my entire Patrol.js

    Code (csharp):
    1.  
    2. #pragma strict
    3.  
    4. public var patrolPoints : Transform[];
    5. public var moveSpeed : float;
    6. private var currentPoint : int;
    7.  
    8. function Start ()
    9. {
    10.    transform.position = patrolPoints[0].position;
    11.    currentPoint = 0;
    12. }
    13.  
    14. function Update ()
    15. {
    16.    
    17.  
    18.    if(transform.position == patrolPoints[currentPoint].position)
    19.    {
    20.      currentPoint ++;
    21.    }
    22.    
    23.    if(currentPoint >= patrolPoints.Length)
    24.    {
    25.      currentPoint = 0;
    26.    }
    27.    
    28.    transform.position = Vector3.MoveTowards(transform.position, patrolPoints[currentPoint].position, moveSpeed * Time.deltaTime);
    29.  
    30. }
    31.  
    Thanks guys!
     
  2. Gilmar

    Gilmar

    Joined:
    Dec 17, 2013
    Posts:
    30
    try adding this
    Code (csharp):
    1.  
    2. Vector3 direction = (patrolPoint[currentPoint].position - transform.position).normalized;
    3. transform.rotation = Quaternion.Euler (direction);
    4.  
     
  3. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398
    Looks good! Thanks!

    But it is telling me UCE0001: ';' expected. Insert semicolon at the end.

    it is pointing to your first line.. which looks fine to me! Am I missing something?
     
  4. unitynoob24

    unitynoob24

    Joined:
    Dec 27, 2014
    Posts:
    398
    OKay I found the issue. should have been patrolPoints not partrolPoint. Simple mistake. This does not seem to be working just yet though...
     
  5. Gilmar

    Gilmar

    Joined:
    Dec 17, 2013
    Posts:
    30
    sorry it should be Quaternion.LookRotation instead of Quaternion.Euler