- Home /

**closed**Nov 25, 2015 at 08:34 PM by TTLMarkwar for the following reason:

The question is answered, right answer was accepted

# Measuring the Angle Between Two Transforms for t Param of Quaternion.Slerp

So, I'm using Quaternion.Slerp to rotate a door hinge, but I'm having a hard time coming up with a good way to measure the t parameter.

I have a door. It has a knob. I have a start transform whose rotation corresponds to the closed rotation of the door and whose position corresponds to the approximate position of the knob when the door is closed. The same is true for my end transform, except for the door being open.

I'd like the door to be manually opened by the player moving their "hand" transform over the doorknob and pulling. From here, I thought I could use the dot product to measure the angle between the vector describing the start transform and the player hand transform.

Rad = cos^-1((A dot B) / (||A||*||B||))

or

`float currentAngle = Mathf.Acos(Vector3.Dot(start.transform.position, tr.transform.position) / (start.position.magnitude * tr.position.magnitude)); float pull = (currentAngle * Mathf.Rad2Deg) / DegreesOfFreedom; `

This isn't working, the angles I'm getting are far too small, so either my math is wrong, or I'm approaching this the entirely wrong way.

I also realize that I'm not accounting for the fact that, for this process to really work, I need to measure the angle ONLY on the two axis that are not the axis of rotation for the door. I figured I could work on that as soon as I get this part figured out.

EDIT:

I'm waiting for someone else to fix error before I can test, but I think I've over complicated the entire problem.

` float currentAngle = Vector3.Angle(start.transform.position, handPos); `

The above code should work just as well.

Regarding zeroing the magnitude of the vector along the rotation axis, it be nice to have a way to present a vector as either a row or column vector (a matrix). It would make make doing the matrix math yourself easier.

I may be missing something but I don't see the rotation point used in your calculation of the angle. Shouldn't you be looking for the angle between `(start.transform.position - rotationpoint.position)`

and `(handPos - rotationpoint.position)`

, rather than between `start.transform.position`

and `handPos`

?

Something like this?

```
float a = Vector3.Distance(start.transform.position, rotationpoint.position);
float c = Vector3.Distance(start.transform.position, handPos);
float angle = $$anonymous$$athf.Acos(1.0f - ( c * c) / ( 2.0f * a *a ));
angle *= $$anonymous$$athf.Rad2Deg;
```

(sorry if there are any typos)

I'm thinking more like this....

```
Vector3 A = start.transform.position - rotationPoint.position;
Vector3 B = handPos - rotationPoint.position;
```

That gives you the A and B vectors to plug into your formula to find the angle between A and B.

I'm just looking at your diagram in the question above, which shows vectors A and B, and a formula for getting the angle between them, and commenting that (in the code in the question) you don't seem to be using the vectors A and B. You seem to be using vectors representing the point at one end of each of them ins$$anonymous$$d.

**Answer** by TTLMarkwar
·
Nov 26, 2015 at 03:48 PM

Like Bonfire said, the problem was I was using the vectors of start and hand, instead of vectors ending at start and hand, and beginning at the rotation point. So it wasn't the math, just my variables. Thanks for your help.

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.