# (unity) Working out Eulers between objects

Hiiiiii

I've googled and googled and I can't figure this out :/ It seems simple enough, but uhhh how do I work out angles among 3D objects? I'm looking for the blue ?? degrees! In Unity!

I've googled and googled and I can't figure this out :/ It seems simple enough, but uhhh how do I work out angles among 3D objects? I'm looking for the blue ?? degrees! In Unity!

Screenshot 2019-02-08 at 19.56.13.png

646 x 584 - 43K

## Comments

Using that, you can find the angle between any vector and any other one.

So working backwards (and untested, so possible typos or whatever)

In this case, we only divide by the magnitude of your other vector, because the global up/down vector has a magnitude of 1.

Keep in mind that the angle that it gives you is probably in radians, but you can multiply by Mathf.Rad2Deg to convert it if necessary.

[edit] Oooh, I've never used the method @Rigormortis posted before. It would make sense that there's a built in method for this... :P Looks like

Vector3.Angle(from, to)is a thing too.https://answers.unity.com/questions/591879/rotation-angle-from-one-object-to-another.html

Vector3 vector = objB.transform.position - objA.transform.position;

float angle = Mathf.Acos(Vector3.Dot(

vector, Vector3.down) /vector.magnitude);Vector3

vector= objB.transform.position - objA.transform.position;@Elyaradine, that method didn't always exist. I can't recall which patch it came in, but I believe it basically does the dot product calculation internally.

Will report back soon >_<

Like @Elyaradine says, what you can do is test the axis's individually, like first how far off on the X axis it the vectors are and then how far off on the Y axis it is, because it sounds like you want to use the angles individually, i.e. calculating the X Angle and the Y angle, and then using the X and Y angles for your Euler Angle on a transform.

Okay let me try my best to explain what I want:

- I have three points. In VR. So there's the head point, and the two hand points.

- The head is pointing where the head is pointing. The hands are where the hands are.

- I have a long "bone" object that has its origin at the head point, pointing directly towards the left hand point.

- I want to rotate the one bone, let's take the left, x degrees away from the facing of the head point. So more x, the further up/away from where the head is looking. Effectively, creating a backward-bending leg. The less x, the straighter it is back to the original "pointing at the hand" position.

- After that I'll do a second bone jointed to the first bone at the bottom that mirrors the first bone's rotation and together they'll be a physics enabled leg. Hopefully :)

Hope that makes sense?

Just as I draw this I realise there might be a problem with this approach but let me get it working first to see if it.... indeed doesn't work :/ HOPE IT DOES!!!

There's a nifty function called the cross product that finds the perpendicular vector (a vector is just a direction in 3D space) given two other vectors, that's how we're going to find the axis to rotate the bone around. You already have the two vectors you need: The head_forward vector and the vector from the head to the hand:

(Also, I'm going to assume you want to do everything in world space - things could be easier in object space if you set up your gameobject hierarchy in certain ways, but you said two hands and world space is the best way to make that stable)

That should work in most cases. All you need to do is animate the relativeRotation value over time and it'll shift the bone around by that many degrees around the axis perpendicular to the hand. If you run it in the update loop it'll even shift the bone relative to hand motion...

Some things that might go wrong:You might have to make the rotation be -30 degrees instead of 30 degrees, because the direction that the "hinge" axis is pointing might be opposite to what you want for a clockwise rotation to work.

If the hand goes above the head that hinge axis might flip as well. But if that happens all you need to do is check to see which is higher and change the order you pass the vectors into the Vector3.Cross function.

Then I tried to debug the values and saw that the forward vector (head.forward or head.transform.forward) were coming out with -1 to 1 values instead of my direction vectors which are 360 values. After some googling, seems transform.forward's values are not the same as a vector derived from this, which is how I'm working out the line/angle from the head point to the arm point:

So my understanding is that if I multiply the rotation of my arm by the the rotation that I want to influence it by (x degrees in the direction of the head's forward (or minus that, whatever)), it should work. BUT THE TWO VALUES ARE DIFFERENT SYSTEMS!

How do I convert transform.forward's normalised value to a Vector3 direction?I've googled the living beep out of this and it doesn't seem to be a thing that people even think about. What's up with that????