x


How to get the positive or negative angle between objects?

I need to decide if I want something to swivel left or right towards my target. Currently, angle only seems to return positive values. I need to find out, if two objects are level, if I need to add angles to the y rotation of my object, or subtract angles in order for it to eventually face towards my target.

Is there a straightforward function in Unity I've overlooked?

more ▼

asked Apr 23 '10 at 10:21 AM

jc_lvngstn 1 gravatar image

jc_lvngstn 1
266 8 9 20

(comments are locked)
10|3000 characters needed characters left

3 answers: sort oldest

You can use Mathf.Atan2 to compute the angle between 2D vectors. This only works for 2d rotations though, however often that's all you need even in a 3d game. Eg:

var localTarget = transform.InverseTransformPoint(target.position);
var targetAngle = Mathf.Atan2(localTarget.x, localTarget.z);

However, the "angle" will be in radians, not degrees, so you most probably want to convert it to degrees before using it, like this:

var targetAngle = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;

This should give you the target angle in the range -180 to 180 degrees.

more ▼

answered Apr 23 '10 at 11:28 AM

duck gravatar image

duck ♦♦
41k 92 148 415

It helps me too. Thanks.

Mar 13 '11 at 11:46 AM mrde

Duck, you're a genius! I spend several hours trying to acomplish angle calculations to rotate enemy towrads waypoint! in 2 damn fantastic lines! 1000 times THANKS!

Jun 13 '11 at 06:44 PM BLF-Games

After DAYS of searching, I found the solution! Thank you, Ben Pitt! Finally somebody answers this question without telling the person that their question "doesn't make sense."

Jul 30 '12 at 07:38 AM SergeantBiscuits
(comments are locked)
10|3000 characters needed characters left

You can test to see if the angle returned it more than 180 and then reverse the rotation your self (-360).

Something like:

function getAngle(){
  // angle code
  if (Angle > 180) Angle -= 360;
  return Angle;
}

// do your rotation
more ▼

answered Apr 23 '10 at 10:33 AM

spinaljack gravatar image

spinaljack
9.1k 18 31 91

Angle will give you a value of at most 180 degrees.

Apr 23 '10 at 02:34 PM Horsman

This the common-sense solution. Some people try too hard.

Mar 22 '11 at 03:08 AM Antitheory
(comments are locked)
10|3000 characters needed characters left

A nice trick for 3d: the cross product of two vectors is a vector quantity with a magnitude equal to the product of the magnitudes of the two vectors times the sine of the angle between them.

So for two normalised vectors:

angle_diff = asin(cross(v1, v2).len())

The resulting vector can be used as an axis for rotation between the two input vectors.

more ▼

answered May 06 '10 at 07:39 AM

_MGB_ gravatar image

_MGB_
678 4 7 16

Thanks for this info, MGB

May 06 '10 at 03:44 PM jc_lvngstn 1

whhhaatt? what does cross product do? why is it useful? How can this be applied to posative / negative angles?

Jan 01 '12 at 04:04 AM 1337GameDev
(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:

x724
x286
x17
x1

asked: Apr 23 '10 at 10:21 AM

Seen: 7110 times

Last Updated: Jul 30 '12 at 07:38 AM