This site uses strictly necessary cookies. More Information

X# Checking if the reflected angle is too sharp

Hi, I'm making a game that is based on ricochet mechanic. There is a bullet that reflects from walls using Vector3.Reflect or the player can swipe to set its direction.

The problem that I faced is that I can't figure out how to check if the reflected angle is too extreme (more than 70 degrees or less than 0 relative to the wall). I've tried dozens of solutions but they either didn't work or worked only half of the time.

Here is the relevant code

```
//Ricochet stuff
void Ricochet(){
if(Mathf.Abs(UserSetAngle()) > 0) fingerRot = UserSetAngle();
//When raycast is hit calculate reflection
if (Physics.Raycast(ray, out hit, Time.deltaTime * speed + .1f, collisionMask)) {
if(Mathf.Abs(fingerRot) > 0) {
Vector3 hitnormal = hit.normal;
transform.eulerAngles = new Vector3(0,fingerRot,0);
fingerRot = 0;
mouseAngle = 0;
}
else{
Vector3 reflectDir = Vector3.Reflect(ray.direction, hit.normal);
float incomingAngle = Mathf.Atan2(reflectDir.z, reflectDir.x) * Mathf.Rad2Deg;
float rot = 90 - incomingAngle;
//Rotate in the right direction
transform.eulerAngles = new Vector3(0,rot,0);
rot = 0;
}
}
}
void GetMouseAngle(){
if (Input.GetMouseButtonDown(0))
{
startPosition = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0))
{
float angle = 0;
Vector3 mouseDelta = Input.mousePosition - startPosition;
angle = 90 - Mathf.Atan2 (mouseDelta.y, mouseDelta.x) * Mathf.Rad2Deg;
if (transform.eulerAngles.y < 180) angle = angle - transform.eulerAngles.y;
if (transform.eulerAngles.y > 180) angle = transform.eulerAngles.y - angle;
if (angle<0) angle += 360;
mouseAngle = angle;
}
}
float UserSetAngle(){
//Mouse and touch controls, saves the input executes at hit
if(mouseAngle>0)
{
if(transform.eulerAngles.y < 180f) fingerRot = transform.eulerAngles.y + mouseAngle;
if(transform.eulerAngles.y > 180f) fingerRot = transform.eulerAngles.y - mouseAngle;
return fingerRot;
}
```

Here's what I would do. I would find the direction from the hit point to the start, then find the angle between that and the reflected angle, something like so :

Vector3 dir = startPosition - hitNormal;

float angleBetween = Vector3.Angle(dir, reflectDir);

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Rotation before 0 and after 360 0 Answers

Enemy not facing player when enter rotation orbit 0 Answers

Object doesn't rotate well with camera raycasting. 0 Answers

Question on rigidbody character rotating to the degree of input and then moving 0 Answers

How would I orient VTOL engines relative to the direction of a ship's movement? 1 Answer