Does Vector.Reflect really do reflect?

Hi all. I found strange issue, may be there is mistake in official doc? Unity - Scripting API: Vector3.Reflect

My code is -

 Vector3 startPoint = transform.position;
        Vector3 secondPoint;
        RaycastHit hit;
        RaycastHit hit2;
        Vector3 rayDir = transform.forward;

        if (Physics.Raycast(startPoint, rayDir, out hit, 300))  //first line  blue
        {
               Debug.DrawLine(startPoint, hit.point, Color.blue);
               rayDir = Vector3.Reflect((hit.point-startPoint), hit.normal);
               secondPoint = hit.point;

              

               if (Physics.Raycast(secondPoint, rayDir, out hit2, 300)) 
               {    Debug.DrawLine(secondPoint, rayDir, Color.green);  // green line ???
                    Debug.DrawLine(secondPoint, hit2.point, Color.red); //second red line
               }
        }

the picture is -

May some one tell me why:

  1. Why green line is not a mirror at all (it uses Vector3.Reflect :frowning: ) ?
  2. Why the second red line doing exactly mirror effect?

Because you’re not passing in a point to draw to for the green line. You’re passing in a direction. If you want to draw a direction, you use Debug.DrawRay.

6 Likes

Im totaly noob… thank you really. May be this example (with right code) should be placed to documentation?

There’s already two code examples for DrawLine and DrawRay. The arguments are described in both too.

You made a mistake is all. You actually use it correctly in several places (Lines 9 and 17) so I’m not sure I follow then why or how the docs should be changed?

1 Like

Yes you right, documentation about DrawRay and DrawLine is good. I mean documentation about “Vector3.Reflect” it only says only “Reflects a vector off the plane defined by a normal.” so why just don add at least some more about same " Debug.DrawLine(Point, point, Color) "; I see many question in old Unity Answers and forums about this makes people confuse. Just my advice to be more clear

The best way to be more clear is to always used named arguments whenever there is ambiguity, eg two Vector3 objects passed as arguments

For instance:

Always use named arguments with Physics.Raycast() because it contains many poorly-designed overloads:

https://discussions.unity.com/t/758115/8

I don’t quite understand what Debug.DrawLine has to do with Vector3.Reflect. Vector3.Reflect is a pure mathematical function. Essentially a basic vector math function. The line you quoted perfectly explains what it does. However there’s another paragraph which even goes more into details:

Maybe you have troubles understanding the difference between a position vector and a direction vector? The documentation of this method can not start teaching basic linear algebra :). If your linear algebra is a bit rusty, I can highly recommend the 3b1b series on linear algebra, at least the first few videos.

1 Like