简体   繁体   English

Unity获取与平面隐藏部分的碰撞坐标

[英]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.我想把那个球体放在指针的 position 上,指针会碰到平面。 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.创建一个新层并将其命名为 MyHiddenLayer 并将您的 object 分配给它。 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))
{
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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