Problem with Vector3.Reflect

Can anybody tell me why my reflected DrawLine isn’t working when using Vector3.Reflect?

// This c# script is attached to the red particles.
//

Vector3 Direction = transform.TransformDirection(Vector3.forward);
RaycastHit RayHit;

if (Physics.Raycast(transform.position, Direction, out RayHit, 1000, layerMask))
{ 	
newPos = RayHit.point;
reflectionAngle = Vector3.Reflect(newPos, RayHit.normal);

Debug.DrawLine(RayHit.point, reflectionAngle); 
//

Many thanks for any help.

I have no experience in this, but is it possible you want the negative reflection angle?

It doesnt seem to reflect in the right angle (as in amount of degrees), and it reflects in the opposite direction of what you want. Might be off base here, just spitballin

Hiya,

…yes, you just described the problem i am having :slight_smile:

Vector3.Reflect should be providing me with the correct reflection angle based on the angle of incidence which as you can see it isn’t.

Or I have my initial code wrong.

Anybody?

Anybody?

As know one has anwsered, i will have a stab at it (although i have never used reflect myself).

I think the results you are having are correct. The Scripting Manual states:

// Makes the reflected object appear opposite of the original object,
// mirrored along the z-axis of the world
reflectedObject.position = Vector3.Reflect (originalObject.position, Vector3.right);

From the above code, It seems that the result is a mirror of the direction vector you supply. So for your code, you are passing in the normal, which would be away from the face(ie, pointing towards the bottom right of the image) - so, the mirror of this would be pointing in the direction you are seeing.

Thats how I read the manual… sorry I can’t be any more help.

Regards,
Matt.

Ooops sorry for double post above.

Thanks for replying. I can see what you mean, its just showing the flipped normal angle rather than the angle of reflection.

Ok so what I need to work out is the angle of reflection myself.

Thanks for the help

Geoff

You are using the hit point instead of the incoming ray direction as parameter to reflect. Try:

reflectionAngle = Vector3.Reflect(Direction, RayHit.normal);

Also note that the returned Vector3 is the reflection direction, not the reflection angle.

Great thanks, that was the solution.

I can’t believe I kept missing that on such a small portion of code. Live and learn :slight_smile:

Thanks again.

Hi, I made asset that is solved your problem.
You can get in on AssetStore: Lazors - Starter kit | Packs | Unity Asset Store

66a388e54327cb884f70b07caf9b248fefa94356.jpeg