Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Discussion Does Vector.Reflect really do reflect?

Discussion in 'Scripting' started by Jicefrost, Sep 3, 2022.

  1. Jicefrost

    Jicefrost

    Joined:
    May 13, 2019
    Posts:
    40
    Hi all. I found strange issue, may be there is mistake in official doc? https://docs.unity3d.com/ScriptReference/Vector3.Reflect.html

    My code is -
    Code (CSharp):
    1.  Vector3 startPoint = transform.position;
    2.         Vector3 secondPoint;
    3.         RaycastHit hit;
    4.         RaycastHit hit2;
    5.         Vector3 rayDir = transform.forward;
    6.  
    7.         if (Physics.Raycast(startPoint, rayDir, out hit, 300))  //first line  blue
    8.         {
    9.                Debug.DrawLine(startPoint, hit.point, Color.blue);
    10.                rayDir = Vector3.Reflect((hit.point-startPoint), hit.normal);
    11.                secondPoint = hit.point;
    12.  
    13.              
    14.  
    15.                if (Physics.Raycast(secondPoint, rayDir, out hit2, 300))
    16.                {    Debug.DrawLine(secondPoint, rayDir, Color.green);  // green line ???
    17.                     Debug.DrawLine(secondPoint, hit2.point, Color.red); //second red line
    18.                }
    19.         }

    the picture is -

    May some one tell me why:
    1) Why green line is not a mirror at all (it uses Vector3.Reflect :( ) ?
    2) Why the second red line doing exactly mirror effect?
     
    Last edited: Sep 3, 2022
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    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.
     
  3. Jicefrost

    Jicefrost

    Joined:
    May 13, 2019
    Posts:
    40
    Im totaly noob... thank you really. May be this example (with right code) should be placed to documentation?
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,321
    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?
     
    hippocoder likes this.
  5. Jicefrost

    Jicefrost

    Joined:
    May 13, 2019
    Posts:
    40
    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
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,519
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,919
    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.
     
    orionsyndrome likes this.