如何使用电影机获取鼠标位置


// Calculate the world position for the mouse.
var worldPos = Camera.main.ScreenToWorldPoint(Input.mousePosition);

这个代码是我用来获取鼠标位置的,但在使用电影机后,这个代码似乎坏了。使用电影机时如何获取鼠标位置?

如果您使用的是多台电影摄影机。你应该更新从目标物体到虚拟相机的距离

我使用";切割";虚拟摄影机之间的更改效果在此处输入图像描述

public class MyController:MonoBehaviour
{
private float distanceToCamera;
private CinemachineBrain cameraBrain;
private void Start()
{
cameraBrain = Camera.main.GetComponent<CinemachineBrain>();
cameraBrain.m_CameraCutEvent.AddListener((brain) =>
{
if (brain != null)
{
if (brain.ActiveVirtualCamera != null)
{
// if virtual camera changed
distanceToCamera = Vector3.Distance(transform.position, brain.ActiveVirtualCamera.VirtualCameraGameObject.transform.position);
Debug.Log($"on cut event {brain.ActiveVirtualCamera.Name} {distanceToCamera}");
}
}
});
// init distance
distanceToCamera = Vector3.Distance(transform.position, Camera.main.transform.position);
}
private void Update()
{
if (Input.GetMouseButton(0))
{
Vector3 mousePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distanceToCamera));
Debug.Log($"mousePosition {mousePosition}");
}
}
}

相关内容

  • 没有找到相关文章

最新更新