Comments and answers for "Transform angle to grid position"
http://answers.unity.com/questions/369599/transform-angle-to-grid-position.html
The latest comments and answers for the question "Transform angle to grid position"Comment by whydoidoit on whydoidoit's answer
http://answers.unity.com/comments/372455/view.html
Yes I know that - which is why using transform.forward doesn't work - you need to measure the angle from straight up the screen. What direction is that? It might be Vector3.forward if up the screen is in + z or it might be Vector3.up if up the screen is measured in + y.
In your diagram - what world coordinate change is incurred to move the red dot one space directly upwards (to your 1,0 square).Tue, 01 Jan 2013 12:24:40 GMTwhydoidoitComment by Celledor on Celledor's answer
http://answers.unity.com/comments/372447/view.html
I'm afraid I don't follow when you say the "real" forward. I'm actually more interested in the square that the soldier is standing on as the Soldier should be able to be anything and be rotated in whatever direction.Tue, 01 Jan 2013 12:00:36 GMTCelledorComment by whydoidoit on whydoidoit's answer
http://answers.unity.com/comments/371999/view.html
Yeah it needs to be using "real" forward not transform.forward (as in the direction vector that in the world implies up the screen). I wasn't aware you were rotating your characters/forgot that you were using transform.forward.Mon, 31 Dec 2012 11:27:33 GMTwhydoidoitAnswer by Golan2781
http://answers.unity.com/answers/371167/view.html
You can determine the relative angular direction of two vectors in the XY-plane using the cross product's Z component (respectively for permuted coordinates). This is apparent if you think of the cross product relation in calculating a surface normal of two vectors or a rotation. If you know you are only working with a 2D plane, this calculation is enough:
var clockwise = true;
if (vec1.y*vec2.x > vec1.x*vec2.y)
clockwise = false;
If you only need world coordinates (i.e. direction 'up' instead of 'forward') you already know one vector for sure (e.g. 'up' is 0,1) so the calculation gets really simple - check whether the horizontal axis (whichever corresponds to -90° and +90°) is positive or negative
Of course, there are other ways to achieve what you want. Assuming you only need world directions:
The arctan of the relative (x/y) will give you the proper angle with proper sign.
You can alternatively try and catch the respective cases directly from the relative coordinates. For example, if y<0 it must be one of the lower fields; you can do this for all coordinates and directons. It would be adviseable to do some kind of normalizing between the two directions, so that for example -9/9 is the same as 1/1 and you probably want treat intermediate values such as 4/3 or 1/0.75 as 'upper right'.
Personally, I'd go for the manual cross product, it should be the most performant solution.Sat, 29 Dec 2012 19:55:06 GMTGolan2781Comment by Celledor on Celledor's answer
http://answers.unity.com/comments/371142/view.html
I made a longer answer but it seems to have disappeared: I uploaded the code and it can be played here: http://www.veus.se/Games/TurnBasedRTS1/TurnBasedRTS.html
"Left click" a soldier and move it around by "right clicking" the ground. The red Squares with a hole in it represents the Squares your code return. In the upper left the angle is printed.Sat, 29 Dec 2012 18:58:36 GMTCelledorAnswer by whydoidoit
http://answers.unity.com/answers/369653/view.html
So you should just be able to take 22.5 from the angle (which is 360/(8*2)), if the result is less than 0 then add 360, then divide the answer by 45 and take the integer as a lookup into an array of coordinates. However Angle returns the angle between a line and another line - which can never be more than 180 degrees. So you also need to know on which side of the red dot, the green dot lies so you can add to the angle.
CS
//Define the angle pairs as shown in your diagram, starting from upwards
Vector2[] angles = new [] { new Vector2(1,0), new Vector2(1,-1), new Vector2(0,-1) ... }
//Get the angle
var angle = targetDir.x < 0 ? 180f + (180f - Vector3.Angle(targetDir, forward)) : Vector3.Angle(targetDir, forward);
//Convert it into an index
var angleIndex = Mathf.FloorToInt((angle < 22.5f ? 360f + angle - 22.5f : angle - 22.5f)/45);
//Get the directions
var direction = angles[angleIndex];Thu, 27 Dec 2012 08:01:26 GMTwhydoidoit