Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Rotate Around Object, in Absolute Angles?

Discussion in 'Scripting' started by Cybearg, Oct 30, 2015.

  1. Cybearg

    Cybearg

    Joined:
    Jan 18, 2015
    Posts:
    46
    Basically, I want the exact functionality of Transform.RotateAround() (object A rotates around object B along specified axis by x degrees), except I want to specify the angle in absolute terms, not relative terms.

    Presently, if you do something like transform.RotateAround(Vector3.zero, Vector3.up, 20), the object would rotate around the world position 0,0,0 along the Vector3.up axis by 20 degrees every time that the method is called. So if I ran the above code 5 times, object A's final angle would be 100 degrees greater than its starting angle.

    Instead, I want it to take the angle in absolute terms, so if I ran transform.RotateAroundAbsolute(Vector3.zero, Vector3.up, 20) five times, the angle of the rotated object would still be 20.

    I haven't been able to find a direct method that will do this, though I'm aware I can use Quaternion.LookAt() to do part of the required functionality: getting the rotated object A to always face the object B that it's rotating around, but how do I actually make object A physically position itself around object B based on an inputted angle?
     
  2. sz-Bit-Barons

    sz-Bit-Barons

    Joined:
    Nov 12, 2013
    Posts:
    150
  3. Cybearg

    Cybearg

    Joined:
    Jan 18, 2015
    Posts:
    46
    Maybe that's a solution as well, though I'm not sure what that would look like.

    I ended up going with a suggestion I got from the chat:
    Code (csharp):
    1. transform.rotation = Quaternion.AngleAxis(angle, Vector3.back);
    2. transform.position = objectB.transform.position + Quaternion.AngleAxis(angle - 90, Vector3.back) * new Vector3(distance, 0, 0);
     
    Egil-Sandfeld likes this.
  4. sz-Bit-Barons

    sz-Bit-Barons

    Joined:
    Nov 12, 2013
    Posts:
    150
    yeah, this is a possible solution.
    Another solution would be: placing the object inside an empty object, shift its local position and then rotate the empty parent object.
     
  5. guerrero4de

    guerrero4de

    Joined:
    May 9, 2016
    Posts:
    1
  6. Cybearg

    Cybearg

    Joined:
    Jan 18, 2015
    Posts:
    46
    If I recall, it was to make the angles relative to a clock-like rotation, with 0 degrees being at 12 o'clock, rather than the standard 0 degrees being 3 o'clock.