x


Quick Angles Question

I have a code where I'm getting an angle between two Vector3's, however the angles returned come in the form of numbers like .011340 or .19872009752 as opposed to 90 degrees or 180 degrees. Can someone tell me what I'm doing wrong? The two variables used here are Vector3's:

generalDirection = Vector3.Angle (transform.position, desiredPosition);

EDIT: Sorry but now the angle starts at 35, goes up to 135 on the other side and goes back down to 35 with a full clockwise rotation. I'm confused.

EDIT: Ok I fixed that, now I just need help making one side negative and the other positive

more ▼

asked Jul 21 '12 at 12:04 AM

Chimera3D gravatar image

Chimera3D
479 16 34 49

aldo's code should work if this script is attached to the object at the vertex of the angle, otherwise replace vectors with the appropriate version of:

position1 - vertexPosition , position2 - vertexPosition

Jul 21 '12 at 12:50 AM Seth Bergman

something like:

if(generalDirection > 180) generalDirection = -(360-generalDirection);

Jul 21 '12 at 01:42 AM Seth Bergman

I've tried that already.

EDIT: Because the value never goes higher than 180 it just get's inverted and starts going back down until hits 0 on the other side.

Jul 21 '12 at 07:53 AM Chimera3D

Nvm figured something else out. Thanks for all the help!

Jul 21 '12 at 07:58 AM Chimera3D
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

Vector3 is a structure used to store many 3 float values, like points, vectors, angles etc. Transform.position and desiredPosition are points, not vectors, and Vector3.Angle is intended to be used with vectors. In this particular case, you're getting the angle between the vectors (0,0,0)->transform.position and (0,0,0)->desiredPosition, which normally makes no sense. If you want to measure the angle between the transform forward direction and the direction from the transform to the desiredPosition, you should use this:

generalDirection = Vector3.Angle(transform.forward, desiredPosition-transform.position);

A point is a position in the 3D world, while a vector is a direction - you can think of a vector as an "arrow" based on 0,0,0 and pointing to the desired direction. In order to calculate the vector that points the direction from position A to position B, subtract A from B:

directionAtoB = pointB-pointA;

The resulting vector shows the direction A to B, and its length (directionAtoB.magnitude) is the distance between A and B.

more ▼

answered Jul 21 '12 at 12:15 AM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

Thanks for that information!

Jul 21 '12 at 12:24 AM Chimera3D
(comments are locked)
10|3000 characters needed characters left

see the script reference here:

http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Angle.html

they use two directions, not two points.. if you think about it, the angle based on two points in space would be relative to the point of origin..

more ▼

answered Jul 21 '12 at 12:17 AM

Seth Bergman gravatar image

Seth Bergman
7k 10 16 28

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2168
x887
x578
x55
x38

asked: Jul 21 '12 at 12:04 AM

Seen: 360 times

Last Updated: Jul 21 '12 at 07:58 AM