【问题标题】:How to compute location of my mouse pointer on some 3D object in Unity?如何在 Unity 中计算鼠标指针在某个 3D 对象上的位置?
【发布时间】:2018-06-18 12:02:23
【问题描述】:

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

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

【问题讨论】:

  • ScreenPointToRay,然后是RayCast?很确定RayCastHitInfo 会返回所有此类数据。

标签: unity3d coordinates mouse coordinate-transformation


【解决方案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.point (docs for hit.point)

【讨论】:

    【解决方案2】:
    猜你喜欢
    • 2015-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多