1
votes

I'm trying to test some code to create Vector2 from its angle and its distance from the player position. I've looked up online how to do this and found the Vector2.Angle function to get the angle between two vectors, and the Vector2.Distance function to find the distance between two vectors, and I thought that I would be able to recreate the original (check_points[i]) Vector using these values, but when I run this code this is what I get (the white lines are from position to check_points[i], and the red lines are from position to other, which is the vector I'm trying to create from the values):

enter image description here

I would have expected the red and white lines to be the same, how can I make them this way?

float angle = Vector2.Angle(position, check_points[i]) * Mathf.Deg2Rad;
float dist = Vector2.Distance(position, check_points[i]);
Vector2 other = new Vector2(Mathf.Cos(angle) * dist, Mathf.Sin(angle) * dist);

Debug.DrawLine(position, check_points[i], Color.white);
Debug.DrawLine(position, other, Color.red);

The point of this is to track the corners of obstacles so I can generate a mesh from the triangles made between the corners and the player, to do line of sight checking. I've previously gotten it to work just casting a ray out for every degree of rotation (0-360), but I've read this tutorial and am trying to implement corner tracking to get it to look smoother: https://ncase.me/sight-and-light/

1
Confused.. I personally would do other as (cos(angle), sin(angle).normalized * distance - BugFinder
@BugFinder doing that I still get similar results - Tom Tetlaw
Hmm. Must admit if you know the vectors in the first place it does see an odd thing to try rework it back out, but Im also surprised how different the angles are - BugFinder
@BugFinder yes its odd but I'm just trying to make it work because I'll need it to create a vertex in the mesh if the raycast doesn't hit, something like this: vertex[i] = (cos(angle) * maxViewDistance, sin(angle) * maxViewDistance) - Tom Tetlaw
@BugFinder and TomTetlaw ... which makes sense somehow ... a position of 0,0,0 basically results in a vector without a direction and magnitude 0 ... so I guess it should be undefined what an angle between e.g. 0,0,0 and 1,2,3 is ... you would need to get an angle between a valid direction vector and your position instead like e.g. Vector3.Angle(Vector3.right, check_points[i] - position) and then later do Vector2 other = position + new Vector2(Mathf.Cos(angle) * dist, Mathf.Sin(angle) * dist); or something like that - derHugo

1 Answers

0
votes

So the issue is that your position and check_point[i] are both positions .. you are not really taking directions here.

As noted in the comments e.g. a position of 0,0,0 wouldn't work at all since this vector has no direction so any angle between this and another position should be undefined.

What you want to do is take directions like Vector2.right and check_points[i] - position.

The next issue was that Vector2.Angle is always positive so you want to rather use SignedAngle here!

float angle = Vector2.SignedAngle(Vector2.right, check_points[i] - position)  * Mathf.Deg2Rad;
float dist = Vector2.Distance(position, check_points[i]);

Then later note that a position vector has one more component than only an Angle and a Distance (which alone is only a direction): A StartPoint. You want to use this vector and start at your position (which now also can be 0,0,0) like

Vector2 other = position + new Vector2(Mathf.Cos(angle) * dist, Mathf.Sin(angle) * dist);

Debug.DrawLine(position, check_points[i], Color.white);
Debug.DrawLine(position, other, Color.red);

As you can see the white lines are exatly covered by the red ones so you only see the red lines recalculated from the angle and distance

enter image description here