- Home /
Rotation angle from one object to another
Hi,
I'm trying to ge the rotation angle from my transform to another object.
Here's a little picture to describe what I mean
What I'm trying to achieve is the following: Get the rotation angle from my transform to another gameobject. So that 0° is forward of my transform (The biggest cube in this case). C#
Please let me know if I haven't explained this good enough :)
Thanks, Andreas.
Answer by diegzumillo · Dec 07, 2013 at 09:40 PM
If I get what you're asking this is a simple task. You want the angle between the direction the first cube is looking and the position of the other cube? Then it's something like
Vector3.AngleBetween( object 1 forward, object 1 position - object 2 position );
Thank you very much :D I knew there were a simple way to achieve this.
I tried using this function, but it always returns a positive value, meaning I cant tell if the object is 35° left or right from me, which - in fact - sucks. So i have to calculate this manually... Thanks Unity.
Any angle between two vectors can be interpreted in two different ways. 35° is the "small" angle and 325° is the "large" angle. Both represent the same angle just the other way round. To define a specific orientation you need to specify a reference frame. Two 3d vectors could be oriented any way in space. Without another reference vector there is no concept of "left" or "right".
Well, thats your way of interpreting it... I'd say If I especially want to calculate the angle in respect to my forward vector and in 2D only, I'd need an indication if its positive or negative... but whatever. I've come up with this code now:
// the position to compare with
var targetPosition = new Vector3();
// use my gameobject's transform to deter$$anonymous$$e fwd vector to target
var localTarget = transform.InverseTransformPoint(targetPosition);
// Use Trig to get my Angle IN RANGE -180 to 180
var targetAngle = $$anonymous$$athf.Atan2(localTarget.x, localTarget.z) * $$anonymous$$athf.Rad2Deg;
"left or right of me" I imagine you are working in 2D then. In 3D left/right don't make much sense and it requires more information, maybe a vector itself. Anyway, maybe an easier way is to just use a cross/vector product. It will be a vector pointing in the z direction, and it will be positive or negative depending on whether the vector is 'to the left or right' so to speak.
Answer by Noxrawr · Dec 07, 2013 at 10:25 PM
Hi, this should work.
Vector3 direction = (target - transform.position).normalized;
float angle = Vector3.Angle (direction, unit.transform.forward);
Answer by UziMonkey · Apr 09, 2017 at 10:44 PM
A simple Vector3.Angle call will work here, but usually when I do this I want to know what the angle between them in relation to some plane. If you were to do this:
Vector3.Angle(transform.forward, other.position - transform.position);
You will get the angle between the two through a plane defined by, I think, the first point and the cross product between the two vectors. If the objects are always on the same plane, that's fine, it'll be the same thing. However if one object is elevated above the grid in your image then the angle will not be the same, and the further the object is away from the grid plane the more wrong the result of his function will be.
So instead you can do the same thing, but project the vectors onto a know plane, in this case a plane defined by Vector3.up.
var angle = Vector3.Angle(
Vector3.ProjectOnPlane(transform.forward, Vector3.up).normalized,
Vector3.ProjectOnPlane(other.position - transform.position, Vector3.up).normalized
);
Maybe I'm not understanding this, but wouldn't this start spitting out funky numbers when the forward was facing up?
Answer by astracat111 · Sep 06, 2021 at 10:21 PM
I know this is really dumb, but I just create a dummy object then do LookAt() and then you just subtract the angle of the first gameObject from the dummy gameObject. This wouldn't be good for operating every single frame though, but you could just have a dummy object for general purposes and to LookAt() then just subtract the transform.eulerAngle.y's from each other.
That's not dumb, that's smart. If it works then no reason to knock it. That being said, having a second object in play opens up for more possibilities for bugs and adds another object to the resource pool. Cost vrs reward so sometimes it's a good plan, and others it's not. Depends on what it is you're trying to do.