Search Unity

World versus Local Axes

Discussion in 'Editor & General Support' started by marty, Jun 7, 2005.

  1. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    When manipulating objects in Unity, it appears that the axes gizmos are only available in local coordinates. Is there a way that I translate and rotate objects in Unity using a world axes gizmo?

    If not, this would definitely be a nice future feature to consider.
     
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Good idea
     
  3. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Could you folks please provide a simple code example showing how to rotate and translate an object along it's own X, Y or Z?

    One line of code for each would be fine - I just need to see the line of code that does the local transform change.

    Thanks!
     
  4. David-Helgason

    David-Helgason

    Moderator

    Joined:
    Mar 29, 2005
    Posts:
    1,104
    This should work... of course if you rotate like this, the order of the rotations matters, and can get you into funny positions... but any one of the following will work nicely.

    First take a direction, and use TransformDirection() figure out what it is in world-space, than rotate around or translate along that direction.

    3D maths aren't really complicated, but take a little getting used to. At the end of the GooBall production you can image we got quick doing it ("now, with the ball stuck on the side of this wall, what direction should it move when I press 'left'?")

    Note: The multiplication with Time.deltaTime is to get an even movement regardless of the framerate which effects how often Update() is called...

    Code (csharp):
    1.  
    2. var angleX = 45.0;
    3. var angleY = 45.0;
    4. var angleZ = 45.0;
    5. var translateX = 1.0;
    6. var translateY = 1.0;
    7. var translateZ = 1.0;
    8.  
    9. function Update() {
    10.   transform.RotateAround(transform.TransformDirection(Vector3.right), angleX * Mathf.Deg2Rad * Time.deltaTime);
    11.   transform.RotateAround(transform.TransformDirection(Vector3.up), angleY * Mathf.Deg2Rad * Time.deltaTime);
    12.   transform.RotateAround(transform.TransformDirection(Vector3.fwd), angleZ * Mathf.Deg2Rad * Time.deltaTime);
    13.  
    14.   transform.position += transform.TransformDirection(Vector3.right) * translateX * Time.deltaTime;
    15.   transform.position += transform.TransformDirection(Vector3.up) * translateY * Time.deltaTime;
    16.   transform.position += transform.TransformDirection(Vector3.right) * translateZ * Time.deltaTime;
    17. }
    18.  
     
  5. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Yippee! Works like a charm, David! Thanks for the feedback and code!

    I'm curious though, why isn't there a simpler way to move or rotate an object on it's local axes in Unity? In other 3D authoring applications and API's I've used, I don't need to do any math to do simple local-axis transforms.
     
  6. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    That's what the transform property is for. Every GameObject has a transform property, which represents the object's position in the game world.

    That's why you call methods on and generally manipulate the transform when you want to do something to the object's position, orientation or scale.
     
  7. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Right, but Unity's transform property seems to require a little math to do rotation on a local axis. It's no biggie (now that I've been shown the light - thanks again, David), but I'm just wondering why there couldn't be a command that explicitly does local-axis rotation, without the need to do any math.

    For anyone following this thread, David's post above works like a charm.
     
  8. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    That's true. Either world-space coordinates through transform.position (and ditto for transform.rotation) or parent-relative coordinates through transform.localPosition (plus localRotation and localScale)
    Note the local versions are relative to the parent object's local axes and not the object itself. (Hint: it can often be useful to position a game object inside an otherwise invisible parent game object in the scene)

    You can use the transform component's TransformDirection to transform an object-local direction to global space:
    Code (csharp):
    1.  
    2. localUpAxis = transform.TransformDirection(Vector3.up);
    3.  
    ... and then use it with RotateAround to rotate the object:
    Code (csharp):
    1.  
    2. transform.RotateAround(localUpAxis, angle_in_radians);
    3.  
     
  9. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Still not working.
     
  10. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    The RotateAroundLocal function was intended for what you want. But we introduced a bug in it which made it rotate around the world axis. Will be fixed in 1.0.1
     
  11. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    Actually, the following seems to work fine for local rotation:

    transform.RotateAround(transform.TransformDirection(Vector3.up), y);

    ... where y is the units to turn.

    Is this just a longhand version of the RotateAroundLocal function?
     
  12. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Yes. RotateAroundLocal is supposed to be the shorthand version of transform.RotateAround(transform.TransformDirection(Vector3.up), y);
     
  13. marty

    marty

    Joined:
    Apr 27, 2005
    Posts:
    1,170
    I'm having trouble with the following not translating on a local all of the time as well:

    transform.position += transform.TransformDirection(Vector3.right) * x;

    Any idea what might cause that?

    I'm curious, why is there no TranslateLocal method?
     
  14. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    Hmm... It seems the ...Local functions need some rethinking. We'll revamp these, so if you use them, there might be some small code changes on your side required in 1.0.1
     
  15. Ajaxamander

    Ajaxamander

    Joined:
    Jul 5, 2005
    Posts:
    36
    I'm used to using raw OpenGL, where you have to multiply the transformation matrices together in a particular order (the multiplication not being commutative and all that...) So that's how I'm accustomed to thinking about 3d transforms.

    So, in the "Translation" component, what order are the parts being applied in? Is it Tranlate, Rotate, Scale? Maybe it would be possible to have several transforms, or to be able to change the order?

    For example, (this might be flawed for this discussion or not make sense, since it's based on "re-drawing" a sphere every frame, instead of having "GameObject") if you rotate a cube 45 degrees, then translate it 10 unites out, and translate another cube 10 units, THEN rotate it 45 degrees, the two cubes will have all faces parallel to each other, but will have a 45 degree arc between them.

    That's the main issue I have with the way Unity's translation component works currently. Am I rambling? Does that make any sense? Would something slightly different be more useful? Am I the only one who cares?
     
  16. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
  17. Ajaxamander

    Ajaxamander

    Joined:
    Jul 5, 2005
    Posts:
    36
    Got it, sad thing is I was actually looking at the new documentation last night when I started wondering about this stuff. Though, how should this kind of thing be dealt with in terms of scene setup? Looking at the component inspector for the transform still just has the 9 fields, without any indication of order, etc. Is that set to change?
     
  18. David-Helgason

    David-Helgason

    Moderator

    Joined:
    Mar 29, 2005
    Posts:
    1,104
    Not for the time being. What you can do is use hierachies to achieve what you want.

    We try to keep Unity simple without taking away power: this seems a good method to get that.

    Don't hesitate to ask for help if you have specific problems with it: I'm sure you will get help to make your projects work.
     
  19. Ajaxamander

    Ajaxamander

    Joined:
    Jul 5, 2005
    Posts:
    36
    Working with an object hierarchy is actually what sent me looking for these other joint types to begin with.

    What I was trying to do is make a replicate-able 'car' type object, with objects like so:

    Car
    óChassis
    óLeftWheel
    óRightWheel
    óetc.

    Where chassises, wheels, et al. are all rigid bodies, connected as expected with Hinge joints. When I hit run, the Chassis and attached wheels, etc, fall to the ground, and continue on together. However, the "Car" object sits still. If I make "Car" also a rigid body, it ends up falling through the ground, and taking the rest of the car with it. It seems like it did this even with the "Kinematic" flag set. (Sorry, I don't have Unity in front of me ATM.) But everything seems to not work.

    What is the correct setup of relationships between these objects?
     
  20. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    There are two ways to model a car:

    1) A single rigid body with multiple colliders for the wheels
    Make sure the wheels dont have a rigid body attached.
    You drive the car by adding a force to the entire car.
    You turn it using anistropic frictional physic materials on the wheels and steering the front wheels left and right .

    This is how the car in the physics demo is made.


    2) One rigid body for the car chassis and 4 rigid bodies for the wheels.
    In order for this to work you need to connect the car chassis with the wheels using hinge joints.

    To get started. Just create a box and add a rigidbody. Create 4 spheres as wheels and add a hinge joint to the wheels and connect the hinge joints to the car chassis.
     
  21. kaedmon

    kaedmon

    Joined:
    Jan 28, 2010
    Posts:
    25


    Is this still the "official" workaround for Unity iPhone 1.7 and 3.0? Thanks, Ben