简体   繁体   中英

Unity get collision coordinate with hidden part of a plane

i have a sphere on a plane that stick to my mouse. i want to place that sphere so the position of the pointer, where the pointer would hit the plane. But the plane is hidden by the ball. It works, but the movement of the ball is noisy. i would like to ignore everything except the plane for the collision. can anyone help?

This is what i do actually:

if (Ball!= null) {
        RaycastHit raycastHit;
        
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out raycastHit, 100f))
        {
            if (raycastHit.transform != null)
            {
                //Our custom method. 
                var x = raycastHit.point.x;
                var z = raycastHit.point.z;
                Ball.pos().get_x().update_value(x);
                Ball.pos().get_z().update_value(z);
                
            }
        }
        
        }

You can do this by using the Layers & Layer Masks. Create a new layer and name it MyHiddenLayer and assign your object to it. Then do:

// Define output
RaycastHit raycastHit;

// Define Ray
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

// Define Layer
int layerMask = 1 <<  LayerMask.NameToLayer("MyHiddenLayer");

if (Physics.Raycast(ray, out raycastHit, 100f, layerMask))
{
    ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM