【问题标题】:Transform: mouse orientation not correct变换:鼠标方向不正确
【发布时间】:2018-09-22 11:54:27
【问题描述】:

我有以下鼠标代码(带有 Unity 的 C#)用鼠标(x、y 和 z)旋转相机。但是当我用鼠标旋转相机时,相机会移动一个偏移量。

void Update()
{
    Turn();
    Thrust();
}

void Turn()
{
    float yaw = turnSpeed * Time.deltaTime * Input.GetAxis("Mouse X");  //Horizontal
    float pitch = turnSpeed * Time.deltaTime * Input.GetAxis("Mouse Y");  //Pitch
    float roll = turnSpeed * Time.deltaTime * Input.GetAxis("Roll") //Roll
    transform.Rotate(-pitch, yaw, -roll) 

}

我希望相机完全随着鼠标的移动而移动(就像 FPS 一样)。这段代码有什么问题?

编辑

从副本中尝试了解决方案。当我在 x 轴上将相机旋转超过 +/- 180 度时,我遇到了万向节锁定问题(左变为右,右变为左)。

public class Movement002 : MonoBehaviour {

public float xMoveThreshold = 1000.0f;
public float yMoveThreshold = 1000.0f;

public float yMaxLimit = 45.0f;
public float yMinLimit = -45.0f;


float yRotCounter = 0.0f;
float xRotCounter = 0.0f;

Transform player;

void Start()
{

    player = this.transform.parent.transform;
}

// Update is called once per frame
void Update()
{
    xRotCounter += Input.GetAxis("Mouse X") * xMoveThreshold * Time.deltaTime;
    yRotCounter += Input.GetAxis("Mouse Y") * yMoveThreshold * Time.deltaTime;
    //yRotCounter = Mathf.Clamp(yRotCounter, yMinLimit, yMaxLimit);
    //xRotCounter = xRotCounter % 360;//Optional
    player.localEulerAngles = new Vector3(-yRotCounter, xRotCounter, 0);
}

}

【问题讨论】:

  • 使用transform.localEulerAngles 比使用transform.Rotate 更有意义,比如鼠标控制。
  • 它带来了一个新问题:云台锁。另请参阅我关于 Unity 中的 Gimbal Lock 的其他问题 stackoverflow.com/questions/52347474/…
  • 请注意,您没有使用我提到的transform.localEulerAngles。您使用了transform.rotation,它们不是一回事。使用来自duplicate 的代码。这是一个 FPS 代码,没有云台锁定问题
  • 好的,此代码是否也可用于 6DOF 游戏(x、y 和 z 轴上的 360 度)?
  • 我不知道。此问题已被标记为重复,您必须自己尝试重复。如果不尝试,您将不知道它是否有效。我注意到您也在使用z 轴,您可能想要将new Vector3(-yRotCounter, xRotCounter, 0); 更改为new Vector3(-yRotCounter, xRotCounter, roll);。它在y 轴上也有角度限制,但如果您不想要它,只需删除yRotCounter = Mathf.Clamp(yRotCounter, yMinLimit, yMaxLimit); .而已。编码愉快!

标签: unity3d 3d mouse


【解决方案1】:

我找到了你的答案,here 此页面包含一个名为 mouselook 的类,它是一个统一资源,您可以通过 Unity 资源演示包免费获得。但如果您愿意,您可以在此处获取脚本。

更多关于你在做什么......从编程的角度来看,将脚本放在相机上会更容易,它使用transform.lookAt()

 Vector3 point = new Vector3();
        Event   currentEvent = Event.current;
        Vector2 mousePos = new Vector2();

        // Get the mouse position from Event.
        // Note that the y position from Event is inverted.
        mousePos.x = currentEvent.mousePosition.x;
        mousePos.y = cam.pixelHeight - currentEvent.mousePosition.y;

        point = cam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, cam.nearClipPlane));
transform.LookAt(point);

你可以把它放在你的相机上,然后试一试。重要的是要注意:

  mousePos.y = cam.pixelHeight - currentEvent.mousePosition.y;

因为从屏幕点转换为您世界中的一个点,并且鼠标 y 在屏幕位置反转,所以这会为您翻转它。祝你好运。

【讨论】:

  • mouselook 脚本非常适合相机和鼠标,但仍然存在云台锁定问题。我想旋转超过 360 度(没有钳位/限制),这给了问题。在带有 transform.lookAt() 的代码中,我收到一个错误,因为未声明“cam”。
  • @JohanKornet 您必须自己为脚本分配摄像头。
  • 是的,我已经为脚本分配了一个摄像头。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-16
  • 1970-01-01
  • 1970-01-01
  • 2019-12-09
  • 2012-12-08
  • 1970-01-01
相关资源
最近更新 更多