- Home /
0-360 Y degree from Vector3.Angle
Having trouble with my pixel-movement code, which I'm pretty sure is having problems with Vector3.Angle Made in java, kind of stumped.
//loc = Vector2, this moves towards the endpoint. //endpoint = Vector2, this is stationary.
var dir_difference : float = Vector3.Angle(Vector3(loc.x, loc.y), Vector3(endpoint.x, endpoint.y));
I want dir_difference at this point to be 0-360, but it's instead returning 6-9, passing the endpoint without changing very much.
The rest of my code should work well enough, since it's splitting it down to a 0-7 int which I use to move the pixel.
//dir = last direction the pixel moved in. 0-7
dir_difference = Mathf.Repeat (Mathf.Round(dir_difference / 45 ) , 8); //should return 0-7
dir_2 = dir_difference;
dir_difference = Mathf.Abs(dir - dir_difference); //total difference between angles. Tells me if it needs to move left or right, and how soon
EDIT: To clarify, I'm looking for something that would return something akin to
225 270 315
180 xxx 000
135 090 045
Answer by Bampf · Jul 23, 2010 at 05:29 PM
The arguments to Vector3.Angle are themselves directions; you seem to be passing in positions.
Try subtracting the two positions to get the vector: vec = endpoint - loc
The length of this vector is the distance between the two points; the "slope" of the vector is the direction to travel. You can then convert this into an angle using the Vector3.Angle method. But, you need a starting angle. For example, you might choose the X-axis as your starting angle:
var angle : float = Vector3.Angle(Vector3.right, vec);
You can then use this angle to assign an integer (0-7) to the direction as you had planned to originally. (Although, that code may also need tweaking. What's dir_2 for? Is dir the last calculated direction?)
I don't quite understand where to go with that - - my math is embarrassingly rusty.
225 270 315 180 xxx 000 135 090 045
You originally asked for integers 0-7 but in your new comment it looks like you are rounding to the nearest 45 degree angle? Also, it's not clear whether you wanted 2 or 3 dimensions- you omit the z argument a few times. Can you clarify? $$anonymous$$aybe explain what you'll be doing with these numbers. Is this for movement around a grid?
Your answer
Follow this Question
Related Questions
How do I find the angle between the players forward vector and the mouse position? 1 Answer
Quick Angles Question 2 Answers
Get Direction from 2 Vectors - and Apply to Transform 0 Answers
Trouble with Vector3.Angle() and RotateAround() 1 Answer
Calculate third Vector3 with two Vector3s and an Angle 1 Answer