im working on an 2D TopDown game and stuck at a point…
At this time i figured out to rotate the crosshair targeted from player to mouseposition in a specific distance.
Now i want to color the crosshair red and grenn if its in a specific angle to the player, its working fine but it ignores the rotation of the player so the angle goes incorrect.
The crosshair is a child of the playerprefab.
Has anybody a idea to fix?
void UpdateCrossHair()
{
//Set crosshair position and rotate around player
Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 center = crosshairTarget.position;
Vector2 direction = mousePosition - center;
Vector2 normalizedDirection = direction.normalized;
crosshair.position = center + (normalizedDirection * CrosshairRadius);
//Color the crosshair by degree around player
float angle1 = Mathf.Atan2(crosshairTarget.forward.y, crosshairTarget.forward.x);
Vector3 dir = crosshair.position - crosshairTarget.position;
float angle2 = Mathf.Atan2(dir.y, dir.x);
float deltaAngle = Mathf.DeltaAngle(angle1, angle2);
if ((deltaAngle <= 2.5 && deltaAngle >= 0.6) || (deltaAngle >= -2.5 && deltaAngle <= -0.6))
{
spriteRenderer.color = Color.red;
} else
{
spriteRenderer.color = Color.green;
}
crosshair.transform.rotation = Quaternion.identity;
Debug.Log(deltaAngle);
}
Subtract playerPosition from crosshairPosition to get a vector pointing from the crosshair to the player. Using this and the player.forward vector you have two vectors to compare. Using Vector2.Angle or Vector2.SignedAngle you can now calculate the angle between the two in regards to the player rotation.
Then continue with checking whether it’s between your desired values.
A bit off-topic, but I would also recommend making these values into constants such that you wont have to write them down multiple times and can easily change them later on if you so desire. Writing numbers directly into the code (unless it’s well known numbers like pi) is considered a bad practice called ‘magic numbers’.
Thank you @Yoreki now it works.
But now, the second if condition will be ignored and i dont see why:
void UpdateCrossHair()
{
//Set crosshair position and rotate around player
Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 center = player.transform.position;
Vector2 direction = mousePosition - center;
Vector2 normalizedDirection = direction.normalized;
crosshair.transform.position = center + (normalizedDirection * CrosshairRadius);
//Color the crosshair by degree around player
Vector2 angle1 = player.transform.position - crosshair.transform.position;
Debug.Log("DEBUG angle1: " + angle1);
Vector2 angle2 = player.transform.up;
float deltaAngle = Vector2.SignedAngle(angle1, angle2);
Debug.Log("DEBUG angle2: " + angle2);
if ((deltaAngle < deg45 && deltaAngle > -deg45) || (deltaAngle > deg135 && deltaAngle < -deg135))
{
spriteRenderer.color = Color.red;
} else
{
spriteRenderer.color = Color.green;
}
crosshair.transform.rotation = Quaternion.identity;
Debug.Log("DEBUG deltaAngle:" + deltaAngle);
}
The first condidtion works so if the crosshair enters the region 45° to -45° it turns to green color.
The second condition (135° to -135°) never be triggered
I can’t think of any number that is less than -135 AND greater than 135… Think about that logic for a second on a number line. It doesn’t make sense. It will never be true.