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

Orbiting around local axis

Discussion in 'Scripting' started by ratamorph, Sep 20, 2008.

  1. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    I'm trying come up with a script that will orbit an object around a target, much like the mouseOrbit script that comes with unity, but I need mine to use the orbiting object's local axes as the rotation axes.

    In mouseOrbit the rotation is calculated this way:

    Code (csharp):
    1. var rotation = Quaternion.Euler(y, x, 0);
    but the problem with this is that it will always rotate around absolute world axis so when I'm directly on top of the object it will start to roll instead of orbit which is not the result I'm looking for.

    so I tried this:

    Code (csharp):
    1. var xRot : Quaternion = Quaternion.AngleAxis(x, transform.up);
    2. var yRot : Quaternion = Quaternion.AngleAxis(y, transform.right);
    3. var finalRot : Quaternion = yRot * xRot;
    But it goes all crazy as I rotate.

    Can anyone give me a hand? I think I'm close I just need to figure out how to properly combine the rotations.
     
  2. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    Ok scratch that, that won't even work when is not freaking out. I guess I'm really stuck here. I want to be able to rotate an object the same way I do in the unity editor just can't find the right way to do it. Anyone know a way that would like to share?
     
  3. ratamorph

    ratamorph

    Joined:
    Sep 2, 2007
    Posts:
    458
    Ok, so I had to use transform.Rotate it was that easy, can't belive I didn't think of it.