我想用鼠标点击3D平面。当我这样做时,我希望它返回我点击的Vector3
。当我使用时:
Vector3 point = Camera.main.ScreenToWorldPoint(Input.mousePosition);
然后,它给了我飞机中心的Vector3
。不是我想要的。我希望它处于我点击的位置。
我正在尝试创建Ray
(Camera.ScreenPointToRay
)并使用Physics.Raycast
,但这只会返回bool
,而不会返回实际点击的位置。
我花了最后3个小时阅读其他人的问题......我在这里错过了什么?
答案 0 :(得分:0)
Plane plane = blablabla;
Ray ray = blablabla;
float distance;
Vector3? intersectionPoint = null;
if (plane.Raycast(ray, out distance))
intersectionPoint = ray.GetPoint(distance);
答案 1 :(得分:0)
这可以帮助你...
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition); //Convert mouse position to raycast
RaycastHit hit; //Create a RaycastHit variable to store the hit data into
Vector3 clickedPosition; //Create a vector3 variable to store the clicked position in
if (Input.GetMouseButtonDown (0) && Physics.Raycast (ray, out hit, 1000)) //If the user clicks the left mouse button and we hit an object with our raycast then
{
clickedPosition = hit.point; //Store the hit position into the clicked position variable
}
就这么简单:)