Search Unity

Rotate Around and Look At

Discussion in 'iOS and tvOS' started by yesimarobot, May 14, 2010.

  1. yesimarobot

    yesimarobot

    Joined:
    Jun 19, 2009
    Posts:
    24
    I'm using the following code to get prototype for moving an object around a spherical object and keeping the object perpendicular to the sphere.

    Code (csharp):
    1.  
    2. var speed = 100.0;
    3. var planet : Transform;
    4.  
    5. function Update () {
    6.     var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed *-1;
    7.     transform.RotateAround(Vector3(0, -512, 0), Vector3.forward, x);
    8.     transform.LookAt(planet);
    9. }
    10.  
    The result is sort of working. I see the following behavior when the object orbiting crosses the halfway point of the sphere. It rotates 180 on the z axis.

     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    The transform.LookAt function has an optional second parameter to specify the upward direction when the object is rotated. This is the direction the object's Y axis will aim towards when its Z axis is pointing in the target direction. If you set the upward direction to be the same as the axis of rotation, you should find the object stays the same way up as it rotates.