如何在Unity中的某个3D对象上计算鼠标指针的位置?

问题描述 投票:0回答:2

是否有任何内置方法来计算与光线相对应的交叉点,表示鼠标指针(或触摸手指)与场景中的某个对象?

例如,如果我有一个球体,我可以在其上计算UV坐标(不自己编写数学)吗?

unity3d coordinates mouse coordinate-transformation
2个回答
1
投票

如果你有一个网格,已经有了UV property。至于从用户点击/触摸的位置发送光线投射,您需要自己做一些工作。

    RaycastHit hit;
    Ray ray = camera.ScreenPointToRay(Input.mousePosition);

    if (Physics.Raycast(ray, out hit)) {
        Transform objectHit = hit.transform;

        // Do something with the object that was hit by the raycast.
    }

我从Unity docs中提取了大量代码。

如果你需要在对象上射击的确切位置,那么你可以使用hit.pointdocs for hit.point


© www.soinside.com 2019 - 2024. All rights reserved.