Search Unity

I don't know how to only reflect in an X-Z axis

Discussion in 'General Discussion' started by satokoentertainment, Nov 6, 2022.

  1. satokoentertainment

    satokoentertainment

    Joined:
    Oct 25, 2022
    Posts:
    6
    I want a reflection of a ray but only in the X-Z axis, while the Y-axis should only allow me to adjust the height of the reflection, but have no impact on the reflection itself;

    Here's the regular reflection:
    Code (CSharp):
    1.  
    2.             if (Physics.Raycast(ray.origin, ray.direction, out hit, remainingLength))
    3.             {
    4.  
    5.  
    6.                
    7.                 lineRenderer.positionCount += 1;
    8.                 lineRenderer.SetPosition(lineRenderer.positionCount - 1, hit.point);
    9.                 remainingLength -= Vector3.Distance(ray.origin, hit.point);
    10.                
    11.  
    12.  
    13.  
    14.                 //temp_normal.x = ray_new.x;
    15.                 ray = new Ray(hit.point, Vector3.Reflect(ray.direction, hit.normal));


    Here's my attempt, I tried making the ray origin's y the same as the hit point's y in hopes that it would automatically give me a X-Z reflection, alas to no avail:
    Code:

    Code (CSharp):
    1.                 var hit_2 = hit ;
    2.                 //Make a new ray with the same y position as the hit point so it doesn't reflect in y
    3.                 var ray_2= new Ray(new Vector3(transform.position.x,hit.point.y,transform.position.z), transform.forward);
    4.                 Physics.Raycast(ray_2.origin, ray_2.direction, out hit_2, remainingLength);
    5.                
    6.                 lineRenderer.positionCount += 1;
    7.                 lineRenderer.SetPosition(lineRenderer.positionCount - 1, hit.point);
    8.                 remainingLength -= Vector3.Distance(ray.origin, hit.point);
    9.                
    10.  
    11.  
    12.  
    13.                 //temp_normal.x = ray_new.x;
    14.                 ray = new Ray(hit.point, Vector3.Reflect(ray_2.direction, hit.normal));
    15.                 if (hit.collider.tag == "Totem")
    16.                 {
    17.                     //lineRenderer.material.color = new Color(0.4f, 0.9f, 0.7f, 1.0f);
    18.                    
    19.                     break;
    20.                 }
    21.  
     
  2. satokoentertainment

    satokoentertainment

    Joined:
    Oct 25, 2022
    Posts:
    6
  3. neginfinity

    neginfinity

    Joined:
    Jan 27, 2013
    Posts:
    13,571
    If your ray hits the ceiling, how is it supposed to behave?

    Anyway, it looks like you're trying to process wall hits and floor/ceiling hits differently andf give the ray a vertical boost.

    To check if you're hitting a wall, verify hit.normal.y . If its absolute values (Mathf.Abs()) is less than 1/sqrt(2) (which is roughly 0.707), you've hit a wall. If it is more than that, that's floor or ceiling.

    Once yhou detected a wall, simply take your reflected ray, adjust its .y component however you want, and then normalize it. And there you go.
     
  4. satokoentertainment

    satokoentertainment

    Joined:
    Oct 25, 2022
    Posts:
    6
    I figured it out by making the .y=0, however this message was surprisingly VERY helpful, ceiling reflections was definitely on my mind and this message helps so much!!