Hi there,

I need to be able to calculate which way an object should reflect off another object.

On first raycast hit I reflect off the normal and if that’s enough to miss the object, great. If it continues to raycast hit the object it will add a set rotation each hit so that the object will definitely steer away from it.

Say for example the object was heading towards a wall rotated at 45 degrees. I need to get a sign (-1 / 1) to apply to the set rotation value (say 30 degrees) so that the object will rotate away from it by 30 degrees, not into it by 30 degrees.

I’ve tried a few approaches with no luck.

Thanks for your time.

Using vector algebra you don’t actually need any trigonometrical functions, you can rotate your object without calculating any angles or rotation values. Once you get your reflected vector, you can use it to “rotate” your forward direction towards the desired direction.

Let me explain that a bit.

First you raycast with your forward direction **A** and check if there’s something to hit.

If there is, you reflect your forward direction around normal **N** and get desired direction **B**. To make a smooth turn, you need to apply some vector calculations.

Every frame find vector **C = B - A** (you should make sure that vector **B** is normalized). This vector **C** will be the **ultimate difference** between your current forward direction and desired forward direction. So if you add vector **C** to your forward direction (**A**), you will be heading exactly at where the vector **B** points.

To make a smooth turn just multiply it, let’s say, by **0.4** (the turn speed) and change your current forward vector to it. So it should look like this:

```
Vector3 C = B - A; // B and A are Vector3, normalized
float turnSpeed = 0.4f;
transform.forward += C * turnSpeed * Time.deltaTime;
```

The closer your **turnSpeed** to 1, the faster it will rotate to the desired direction. However, don’t try to make your turnSpeed more than 1, it won’t make sense.

Cheers.