- Home /

# Angle between vectors - calculation

Hi everyone, i've created a script which gets me some strange values, hope someone can help me.

I want to calculate an angle between two Vector3's. One of them should only include the x-component of the object workplace (left picture), and the other one should be the resulting vector xy of the gameobject welding torch (right picture).

So I created the vectorWorkplace_x (see script) and the resultingVector_xyz (which I put in update because this object is movable) and calculated the angle. As result I get values between approximately 18 - 24. Im pretty sure thats not the angle in degrees between these vectors.

So is this calculation correct? And how do I get the angle in degrees? Thanks in advance! :)

Workplace / weldingTorch

**Answer** by JVene
·
Nov 11, 2018 at 09:57 PM

I have deleted my previous answer, as it was mostly questions to clarify the discussion.

Now that I have more to work with, I should begin with this message: you've oversimplified the problem, at least with the mathematical approach you've been considering. You got the result from Vector3.Angle that was correct for the data supplied to it, but the code there did not actually express the problem you're solving.

Here let me describe the difference between what you've been thinking and the problem you've hinted at in the last post (appended as a question with images). If you take the x coordinate of the work place and compare that to the x/y position of the torch, then ONLY the position of the torch could produce a change in angle. Rotating the torch should produce almost no change in the angle (if the x/y position were the center of the torch). However, in contradiction to what I believe you require, if you MOVED the torch, say a very large increase in y, the angle of the calculation you describe would change greatly, even if you did NOT rotate the torch at all. I don't think this is what you want.

I think what you're looking for is the angle of the torch tip, without regard to the relative position of the torch to the work plane. If, for example, you tilted the torch to a perfect 45 degree angle, then you should receive a 45 degree result no matter where the torch is in relation to the work plane's x (or y or z) coordinates. If I'm wrong about that, then we need to talk more about what you're really looking for.

If I'm right, however, then what you require is a surface normal of the work plane. This is similar, in many ways, to the calculation of light in phong or blinn shading, where if the light is striking the plane at the perpendicular, the light is brightest, and as that angle increases, the light contribution decreases toward zero at a 90 degree (or worse) angle. Your calculation, however, may be more properly the 90 degree complement. The surface normal is like a pole inserted into the work plane, and is perpendicular to the plane of the work surface. The angle of the torch should be compared to this surface normal, not some arbitrary coordinate within the plane. Further, you'll need to make this coordinate comparison local to the surface normal (as if the torch were pointing to the base of that pole that is the normal vector), so that the angle calculated has no relationship to the relative position of the torch, only the angle of the torch. That said, that vector you must use for the torch is not ON the torch. It is a reverse projection, as if you had a pointer away from the torch pointing exactly aligned with the torch.

A surface normal is created by taking two vectors from a plane (usually two sides of a plane), and performing a cross product of them. The result is a vector perpendicular to the plane described by the two source vectors. It projects from the point at which the two source vectors are joined. This is the mathematical model you should be considering. I'll leave the rest to a bit of searching, on cross products of vectors, if the angle between a vector and a plane.

Yes that's exactly what I was looking for. So that's a little more complicated than I initially thought. However I've understood youre explanation and mathematical approach I have to make now. So big thanks for that.

I'll do some research on that and hopefully can achieve what I'm looking for.

**Answer** by Julian_Sch
·
Nov 11, 2018 at 07:34 PM

Thanks again for the detailed response! Hope you can see the pictures now.

First picture includes the values of the calculation I get during game mode.

Yes you're right, I want to calculate the angle of the torch's pitch relative to the work surface (picture 2, angle alpha). The workplace is static and the welding torch can be picked up and is movable (with VR-Controller).

For the simulation I'm creating, only the rotation painted with the black arrow is relevant. Because of that I'm only getting the x- and y-component of the welding torch's vector inside the script.

The gameObject which I attached the script to is the wire of the welding torch (the peak). So it's a child of the whole welding torch and therefore I assume these are local coordinates.

Hope it's more clear now. :)

### Your answer

### 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.