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 Reflecting Rays at User Defined Angles

Discussion in 'Scripting' started by JD_Designer, Feb 25, 2021.

  1. JD_Designer

    JD_Designer

    Joined:
    Sep 1, 2015
    Posts:
    111
    Hello!
    I'm working on a "reflecting" project. I'm casting a ray from my A vector that sits at (0,0,0) towards some other "reflector object" B that is positioned at (some vector x, 0, some vector z). All good so far. Now I want to place some logic on my B object that does the following: Takes the direction vector from the incoming A raycast hit, then casts two rays and line renderer lines given a user defined angle X. I should note that my objects reside in 3D space but y vectors are of no consequence here.

    I searched the forums and found this but it's not quite what I need. I imagine I need to use Vector3.Angle or Vector3.Reflect or Mathf.Deg2Rad but I'm not sure how to apply these. Any help is appreciated. Thanks in advance!

    AngleDiagram.PNG
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,336
    If I understand your issue, you want B to take the inbound angle of the red vector from A and change it by a certain number of degrees to go back out on a different axis.

    And also I believe you're standing above this looking down on the world, so X and Z are the salient axes.

    IF that is the case, then the vector A->B can be bounced directly back at A by rotating it 180 degrees around the Y axis.

    Code (csharp):
    1. Vector3 fromAToB = (positionB - positionA).normalized;
    2.  
    3. Vector3 shootingDirectlyBack = Quaternion.Euler( 0, 180, 0) * fromAToB;
    That's it for that one.

    NOW... if you want it X degrees left or right, then you would add or subtract that from 180 and repeat the above operation:

    Code (csharp):
    1. // the two green rays above
    2. Vector3 greenLine1 = Quaternion.Euler( 0, 180 + x, 0) * fromAToB;
    3. Vector3 greenLine2 = Quaternion.Euler( 0, 180 - x, 0) * fromAToB;
    I normalized the A->B vector so you can just multiply it by however much distance you want, or denormalize it. Everything else continues to work.
     
    JD_Designer, Bunny83 and Joe-Censored like this.
  3. JD_Designer

    JD_Designer

    Joined:
    Sep 1, 2015
    Posts:
    111
    Thank you very much Kurt! This worked perfectly! Here is my implementation.
    Code (CSharp):
    1.  
    2.     public void ReflectBeam(Vector3 inboundBeam)
    3.     {
    4.         Vector3 outboundBeam_1 = Quaternion.Euler(0, 180 + reflectAngle, 0) * inboundBeam * endLine;
    5.         Vector3 outboundBeam_2 = Quaternion.Euler(0, 180 - reflectAngle, 0) * inboundBeam * endLine;
    6.  
    7.         line_1.SetPosition(0, transform.position);
    8.         line_1.SetPosition(1, outboundBeam_1);
    9.         line_2.SetPosition(0, transform.position);
    10.         line_2.SetPosition(1, outboundBeam_2);
    11.  
    12.         RaycastHit hit;
    13.         if (Physics.Raycast(transform.position, outboundBeam_1, out hit))
    14.         {
    15.             line_1.SetPosition(0, transform.position);
    16.             line_1.SetPosition(1, hit.point);
    17.         }
    18.         if (Physics.Raycast(transform.position, outboundBeam_2, out hit))
    19.         {
    20.             line_2.SetPosition(0, transform.position);
    21.             line_2.SetPosition(1, hit.point);
    22.         }
    23.     }
     
    Kurt-Dekker likes this.