Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How to understand Vector3.Reflect() properly?

Discussion in 'Scripting' started by BPPX, Oct 1, 2023.

  1. BPPX

    BPPX

    Joined:
    Apr 16, 2017
    Posts:
    9
    Why is the Result object so far away? I don't understand the meaning of the InNormal parameter.

    I've read it. https://docs.unity3d.com/cn/2021.3/ScriptReference/Vector3.Reflect.html,But it still confuses me and I seem to have misunderstood somewhere.

    Code (CSharp):
    1.  
    2. public class Reflect : MonoBehaviour
    3. {
    4.     public Transform A;
    5.  
    6.     public Transform B;
    7.  
    8.     public Transform InDirection;
    9.  
    10.     public Transform Result;
    11.  
    12.     // Update is called once per frame
    13.     private void Update()
    14.     {
    15.         var inNormal = B.position - A.position;
    16.         var result = Vector3.Reflect(((B.position + A.position) / 2) - InDirection.position, inNormal);
    17.         Result.position = result;
    18.         Debug.DrawLine(InDirection.position, result);
    19.         Debug.DrawLine(A.position, B.position, Color.cyan);
    20.         Debug.DrawLine(InDirection.position, (B.position + A.position) / 2, Color.yellow);
    21.         Debug.DrawLine(result, (B.position + A.position) / 2, Color.yellow);
    22.     }
    23. }
    24.  
    Snipaste_2023-10-01_20-17-38.png
     
  2. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,917
    The result seems so far away since it counts as a direction. It shows the "bounce" direction if you shoot a light ray from your highlighted cube down to that A/B center point. It's like if you ask someone which way is North -- they could just point with a finger, or use their whole arm and it's the same thing: "North is that way". Exactly where their finger tip is doesn't matter. Once you have the result, you can easily rescale it to the length you want, or use it in a raycast to see what it hits, and so on.

    To understand inNormal, hide that silver plane from the image. Now we're bouncing off the point A/B center. Hmmm ... obviously, we also need to know which way the mirror is aimed -- a mirror wall facing right, or forward? a mirror floor? some other angle? inNormal is what tells Reflect which way the mirror is facing. For example, if the floor is a mirror, use Vector3.up. If the mirror is a wall facing right, use Vector3.right. If the mirror is a flat plane, p1, that the user can turn, use p1.rotation.eulerAngles.y.
     
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    541
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Reflect : MonoBehaviour
    4. {
    5.     public Transform A;
    6.     public Transform B;
    7.     public Transform InDirection;
    8.     public Transform Result;
    9.     private void Update()
    10.     {
    11.         var inNormal = (B.position - A.position).normalized;
    12.         var result = Vector3.Reflect(A.position-InDirection.position, inNormal);
    13.         Result.position = result+A.position;
    14.         Debug.DrawLine(InDirection.position,A.position,Color.yellow,0.1f);
    15.         Debug.DrawLine(A.position,Result.position,Color.yellow,0.1f);
    16.     }
    17. }
    18.  
     
    BPPX likes this.
  4. BPPX

    BPPX

    Joined:
    Apr 16, 2017
    Posts:
    9
    Thank you, I made a mistake. I forgot to add Offset.:
    Result.position = result+A.position;

    Now, I wrote the code again and it works perfectly!

    Code (CSharp):
    1. public class Reflect : MonoBehaviour
    2. {
    3.     public Transform InDirection;
    4.  
    5.     public Transform Plane;
    6.  
    7.     public Transform Result;
    8.  
    9.     private void Start()
    10.     {
    11.         Debug.Log(Plane.up);
    12.         Debug.Log(Plane.position);
    13.     }
    14.  
    15.     private void Update()
    16.     {
    17.         var inNormal = Plane.up;
    18.         var result = Vector3.Reflect(Plane.position - InDirection.position, inNormal);
    19.         Result.position = result + Plane.position;
    20.         Debug.DrawLine(InDirection.position, Plane.position, Color.yellow, 0.1f);
    21.         Debug.DrawLine(Plane.position, Result.position, Color.yellow, 0.1f);
    22.         Debug.DrawRay(Plane.position, Plane.up * 10, Color.black);
    23.     }
    24. }
    Snipaste_2023-10-02_12-26-41.png
     
    Last edited: Oct 2, 2023
    Strom_CL and zulo3d like this.