在Unity3D中获取鼠标坐标并转化为世界坐标时,使用:

using UnityEngine;
using System.Collections;

public class MeshScript : MonoBehaviour {
    
    Vector3 mouseScreenPos, mouseWorldPos;
    // Update is called once per frame
    void Update () 
    {    
        // for mouse
        mouseScreenPos = Input.mousePosition;
        print(mouseScreenPos);
        mouseScreenPos.z = 1.0f;
        mouseWorldPos = Camera.main.ScreenToWorldPoint(mouseScreenPos);
        print(mouseWorldPos);
    }
}

出现错误:NullReferenceException
UnityEngine.Camera.ScreenToWorldPoint (Vector3 position) (at C:/BuildAgent/work/14194e8ce88cdf47/Runtime/ExportGenerated/Editor/UnityEngineCamera.cs:267)
MeshScript.Update () (at Assets/MeshScript.cs:38)

原因:没有相机被设为主相机。

解决方法:将其中一个相机的Tag由Untagged改为MainCamera,问题解决。

相关文章:

  • 2021-12-15
  • 2021-08-29
  • 2021-10-22
  • 2022-12-23
  • 2021-09-22
  • 2021-07-01
  • 2021-08-02
  • 2021-12-16
猜你喜欢
  • 2021-05-18
  • 2021-05-26
  • 2022-12-23
  • 2021-10-26
  • 2021-12-10
  • 2021-12-15
  • 2022-12-23
相关资源
相似解决方案