Search Unity

Transforming an object on its local axis.

Discussion in 'Scripting' started by marty, Jun 8, 2005.

  1. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Does anyone know how I transform an object on its local axis?

    For instance, I now that the following line of code will transform an object on the world's Z-axis:

    transform.localPosition.z += 1;

    ... but how do I tell an object to move forward by 1 unit on it's own Z-axis?
     
  2. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    transform.position += transform.TransformDirection (Vector3.fwd);
     
  3. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Thanks!

    Now, how do I tell an object to rotate one unit on its own axes?

    :wink:
     
  4. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    // Rotates the transform angle radians around axis in local space.
    transform.RotateAroundLocal (Vector3.up, Time.deltaTime);

    See the Transform script reference.
     
    Carter_Knighted likes this.
  5. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Man, I am cursed.

    I've been using that same code but I get rotation around the world axis when I do. And I know that can't be right, since it is "transform.RotateAroundLocal" after all.

    If I make a cube, rotate it on one of its axes so that is tilted, then attach this code to it, I get rotation on what appears to be the world Y axis, not the cube's Y axis (which is what I want):

    function Update () {
    transform.RotateAroundLocal (Vector3.up, Time.deltaTime);
    }

    I'm really puzzled. Any idea what could be wrong?

    Thanks again for your help!
     
  6. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Ahh - seems RotateAroundLocal rotates around the PARENT's local space...

    What you're looking for is:

    transform.RotateAround (transform.TransformDirection (Vector3.up), Time.deltaTime);

    This moves the Vector3.up from local to world space. The you rotate around THAT in world space. This will work no matter what your parenting seetup is.
     
  7. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    It's still not working.
     
  8. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Turns out this is a bug in RotateAroundLocal. Sorry about that. Will be fixed in the 1.0.1
     
  9. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Whew!

    So, I'm not insane after all. :wink:

    Any idea when 1.0.1 will be out?
     
  10. Krispy

    Krispy

    Joined:
    Jun 6, 2005
    Posts:
    69
    Any ideas on how to move an object on a different object's local axis? I'm basically trying to set up camera relative controls.
     
  11. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    I'd do it like this:

    1. Make a non-rendering dummy object.

    2. Set the transform of the dummy equal to the transform of the object whose axis you want to move along.

    3. Child the object to be moved to the dummy.

    4. Translate the dummy on the axis you want to move along.
     
  12. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Use 1.0.1 beta 2.

    It has a function:
    Move (Vector3 movement, Transform relativeTo);
     
  13. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Or you could use that new Move command. :wink:

    Boy, those Unity Elves sure are adding all sorts of cool transform commands!
     
  14. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Oh actually the function is called Translate not Move. Sorry for the confusion.
     
    Setting_Sun likes this.
  15. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Yeah, I noticed that in the new documents.

    The new transform commands seem very streamlined now. But I still think it would be a bit cleaner to marge RotateAround into the new Rotate by adding Space=(named object) to the parameter options for Rotate. :wink:

    Also, the HOWTO's added to the new documentation do not make use of the new transforms and LookAt commands, which could. For instance, the "surface tracker" HOWTO does not use LookAt, which it could.
     
  16. Krispy

    Krispy

    Joined:
    Jun 6, 2005
    Posts:
    69
    Interesting, but I need to use Addforce. I'm making a simple (kinda) marble (almost) game, the view is 3rd person (obviously), and I need camera relative controls. The thing is, the player needs to be able to move the camera around the hero.
     
  17. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Code (csharp):
    1.  
    2. cameraRelativeFwd = Camera.mainCamera.transform.TransformDirection (Vector3.fwd);
    3. rigidbody.AddForce (cameraRelativeFwd);
    4.  
    Note that when using camera relative controls you might want to use this instead:

    Code (csharp):
    1.  
    2. cameraRelativeFwd = Camera.mainCamera.transform.TransformDirection (Vector3.fwd);
    3. cameraRelativeFwd.y = 0;
    4. cameraRelativeFwd = cameraRelativeFwd.normalized;
    5. rigidbody.AddForce (cameraRelativeFwd);
    6.  
    This will make sure that only x, z axis have influence on the fwd direction no matter how far above the player you place your camera.
     
  18. Krispy

    Krispy

    Joined:
    Jun 6, 2005
    Posts:
    69
    Thank you so much! Ah, this is why I love these forums.
     
  19. Krispy

    Krispy

    Joined:
    Jun 6, 2005
    Posts:
    69
    Hmm, I'm getting this error:
    " 'UnityEngine.Camera' does not contain a definition for 'TransformDirection' "
     
  20. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Made a small typo. I updated the script above. I forgot to use the transform of the camera instead of the camera.
     
  21. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Also, "AddFoce" should read "AddForce".