Search Unity

rotating object around a point

Discussion in 'Scripting' started by TheRaider, Apr 24, 2011.

  1. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    I am trying to rotate an object around a point. I want the user to be rotate both vertically and horizonatly so with those controls the user can position the light around the object in any place they wish.


    I am using (just rotating around the origin for now.

    transform.RotateAround (Vector3(0,0,0), Vector3.up, 40 * Time.deltaTime);

    and

    transform.RotateAround (Vector3(0,0,0), Vector3.forward, 40 * Time.deltaTime);

    Now both work if you don't touch the other. But as soon as one moves the other doesn't for the new position and doesn't behave the way I want.

    Is there a better way to do this?

    If i could move the pivot to the origin I could just straight rotate but I don't know how to do that, or if it is even a good solution.
     
  2. Tom163

    Tom163

    Joined:
    Nov 30, 2007
    Posts:
    1,290
    The easiest way is to parent your object to an empty gameObject located at the pivot, and rotate that. Always works like a charm for me.
     
    th3z0d1ac likes this.
  3. sandbaydev

    sandbaydev

    Joined:
    Aug 9, 2013
    Posts:
    104
    How about something like this (saw this code here in the forums: http://forum.unity3d.com/threads/47353-Rotate-the-camera-around-the-object )
    Code (csharp):
    1.  
    2.     public Transform target;
    3.  
    4.     void Update () {
    5.         transform.LookAt(target);
    6.         if (Input.GetMouseButton(1))
    7.         {
    8.             transform.Translate(Vector3.right * Time.deltaTime);
    9.         }
    10.     }
    11.