Home > Back-end >  Unity3D: I don't know how to only reflect in the X-Z axis
Unity3D: I don't know how to only reflect in the X-Z axis

Time:11-07

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:enter image description here



            if (Physics.Raycast(ray.origin, ray.direction, out hit, remainingLength))
            {
 
 
               
                lineRenderer.positionCount  = 1;
                lineRenderer.SetPosition(lineRenderer.positionCount - 1, hit.point);
                remainingLength -= Vector3.Distance(ray.origin, hit.point);
               
 
 
 
                //temp_normal.x = ray_new.x;
                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:enter image description here Code:

var hit_2 = hit ;
                //Make a new ray with the same y position as the hit point so it doesn't reflect in y
                var ray_2= new Ray(new Vector3(transform.position.x,hit.point.y,transform.position.z), transform.forward);
                Physics.Raycast(ray_2.origin, ray_2.direction, out hit_2, remainingLength);
               
                lineRenderer.positionCount  = 1;
                lineRenderer.SetPosition(lineRenderer.positionCount - 1, hit.point);
                remainingLength -= Vector3.Distance(ray.origin, hit.point);
               
 
 
 
                //temp_normal.x = ray_new.x;
                ray = new Ray(hit.point, Vector3.Reflect(ray_2.direction, hit.normal));
                if (hit.collider.tag == "Totem")
                {
                    //lineRenderer.material.color = new Color(0.4f, 0.9f, 0.7f, 1.0f);
                   
                    break;
                }
 

CodePudding user response:

Not sure if I understood the question correctly but as I read it you want to reflect from the hitpoint only the XZ axis and ignore Y.

So you could do exactly that

var previousDirection = ray.direction; 
previousDirection.y = 0; 

ray = new Ray(hit.point, Vector3.Reflect(previousDirection, hit.normal));

or in case the hit.normal is also not exactly XZ aligned additionally

var previousDirection = ray.direction; 
previousDirection.y = 0; 
var xzNormal = hit.normal;
xzNormal.y = 0;

ray = new Ray(hit.point, Vector3.Reflect(previousDirection, xzNormal));

Or you could probably go the other way round and rather simply flatten the result of Reflect

var newDirection = Vector3.Reflect(ray.direction, hit.normal);
newDirection.y = 0;

ray = new Ray(hit.point, newDirection);

CodePudding user response:

Below ray_direction I have added a comment of which I think you need. As the ray you have drawn with the line renderer comes right out of the cube, assuming that is the local y direction of that cube.

Also assuming, I think you want it ray parralel to the ground.

RaycastHit hit;
Vector3 ray_start     = transform.position; // this already contains the y position you want.
Vector3 ray_direction = transform.forward;  // the local forward of this gameObject.transform
//                      Vector3.forward;    // if you whish the global forward direction

if( Physics.Raycast(ray_start, ray_direction, out hit, remainingLength) )
{

    // Do something.

}

The Vector3.forward is always the global direction, thus Vector3(0,0,1);

The Transform.forward of an instance is the same as transform.rotation * Vector3.forward. Which means, that vector is rotation by the gameObject's rotation.

  • Related