Search Unity

rigidbody.velocity.z - about to give up

Discussion in 'Scripting' started by ceereeo, Nov 5, 2006.

  1. ceereeo

    ceereeo

    Joined:
    Nov 5, 2006
    Posts:
    3
    Hi, brand new to this.

    I'm trying to use this patrol script I found on this forum and I can't get it to work. It's supposed to make my toon move forward, stop, rotate, then continue in a new direction. The problem is it always continues in the same direction, regardless of which way it's facing. Also, even if I change the original facing of my rigidbody, it still goes in that same direction. Here's my script:

    var walkingDistance : int = 0; // set this in the inspector
    var turnAmount : int = 0; // set this in the inspector
    var walkingSpeed : int = 0; // set this in the inspector
    private var isTurning : boolean = false;
    private var counter : int = 0;

    function FixedUpdate ()
    {
    if (!isTurning)
    {
    // move the robot forward
    rigidbody.velocity.z = walkingSpeed;
    counter++;
    if (counter >= walkingDistance)
    {
    isTurning = true;
    counter = 0;
    }
    }
    else
    {
    // turn the robot
    rigidbody.velocity.z = 0;
    transform.Rotate(0, 1, 0);
    //transform.Rotate(0, 1, 0,Space.World);
    counter++;
    if (counter >= turnAmount)
    {
    isTurning = false;
    counter = 0;
    }
    }
    }
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    rigidbody.velocity is in world space. You are setting the z component of it to a value every frame. Your intention i presume is to change it so set the velocity along the z-axis of the transform. For this you use transform.TransformDirection.

    See below the fixed script. Welcome to Unity.

    Code (csharp):
    1.  
    2. var walkingDistance : int = 10; // set this in the inspector
    3. var turnAmount : int = 10; // set this in the inspector
    4. var walkingSpeed : int = 10; // set this in the inspector
    5. private var isTurning : boolean = false;
    6. private var counter : int = 0;
    7.  
    8. function FixedUpdate ()
    9. {
    10.     if (!isTurning)
    11.     {
    12.         // move the robot forward
    13.         rigidbody.velocity = transform.TransformDirection(0, 0, walkingSpeed);
    14.         counter++;
    15.         if (counter >= walkingDistance)
    16.         {
    17.             isTurning = true;
    18.             counter = 0;
    19.         }
    20.     }
    21.     else
    22.     {
    23.         // turn the robot
    24.         rigidbody.velocity.z = 0;
    25.         transform.Rotate(0, 1, 0);
    26.         //transform.Rotate(0, 1, 0,Space.World);
    27.         counter++;
    28.         if (counter >= turnAmount)
    29.         {
    30.             isTurning = false;
    31.             counter = 0;
    32.         }
    33.     }
    34. }
    35.  
     
  3. ceereeo

    ceereeo

    Joined:
    Nov 5, 2006
    Posts:
    3
    Thanks so much, that did it.