Why is Vector2.SignedAngle not working as expected?
I'm creating a method that casts rays in a "2D cone" in from an origin point, towards a direction, with an angle that determines how wide the cone is and a number of rays to cast within the angle.
I also have a distance and a layerMask so i can do some filtering.
public static RaycastHit2D[] RaycastRadial(Vector2 origin, Vector2 direction, int numberOfRays, float angle, float distance, int layerMask)
{
if (numberOfRays <= 0)
throw new System.ArgumentOutOfRangeException("Number of rays cannot be zero or negative.");
if (numberOfRays == 1)
Debug.LogWarning("Using a method meant for casting multiple rays to only cast a single one, use RaycastSingle instead");
direction.Normalize();
float halfAngle = angle / 2;
float directionAngle = Vector2.SignedAngle(origin, origin + direction); // Find angle of the direction where 0 is downwards.
directionAngle += 90f; // Allign 0 to the right because of a function i'm using later.
Debug.Log("directionAngle = " + directionAngle);
List<RaycastHit2D> hits = new List<RaycastHit2D>();
for (int i = 0; i < numberOfRays; i++)
{
// Calculate the new directions angled from the initial direction.
// DegreeToVector2 is an extension method that turns an angle(degrees), where 0 is to the right, to a vector with magnitude 1. Have confirmed this to work as well.
Vector2 newDirection = ((directionAngle - halfAngle) + (angle / (numberOfRays - 1) * i)).DegreeToVector2();
Debug.Log("newDirection = " + newDirection);
hits.Add(RaycastSingle(origin, newDirection, distance, layerMask));
}
return hits.ToArray();
}
When i run this method every frame and check what happens by drawing the rays, i get some strange interaction with the world origin and i cannot understand why this happens. The method works perfectly if the origins 'x' value is exactly 0.
For some odd reason getting close to the world origin flip the ray's direction on the y-axis.
This looks way more organized in my VS, don't know why it compacts everything like that.
Your answer
![](https://koobas.hobune.stream/wayback/20220613033323im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Mathf.Round ? [C#] 2 Answers
InverseLerp for vector3 1 Answer
Separate Numbers ? 1 Answer
Mathf.SmoothStep sequence Time problem 1 Answer
CS0116 Error 1 Answer