admin管理员组文章数量:1278912
Can somebody tell me what am I doing wrong? Whenever I tilt my object left or right on Z axis (it has rigidbody) my mouse look is inverted until Z axis is 0 again. Two scripts are seperate but on the same empty object.
My TiltMovement
void TiltMovement()
{
float tiltRight = Input.GetKey(GameManager.Instance.InputManager.GetKey("TiltRight")) ? 1 : Input.GetKey(GameManager.Instance.InputManager.GetKey("TiltLeft")) ? -1 : 0;
if (tiltRight != 0)
{
// Calculate the rotation amount based on the tilt input
float tiltAmount = tiltRight * tiltSpeed * Time.deltaTime;
// Apply Z-axis rotation in local space
transform.Rotate(Vector3.forward, -tiltAmount, Space.Self);
}
My Mouse Look
void HandleMouseLook()
{
float mouseX = Input.GetAxis("Mouse X") * lookSpeed * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * lookSpeed * Time.deltaTime;
targetMouseDelta = new Vector2(mouseX, mouseY);
currentMouseDelta = Vector2.Lerp(currentMouseDelta, targetMouseDelta, smoothFactor * Time.deltaTime);
rotationY += currentMouseDelta.x;
rotationX -= currentMouseDelta.y;
rotationX = Mathf.Clamp(rotationX, -clampAngle, clampAngle);
// Apply rotations in local space
transform.Rotate(Vector3.up, rotationY, Space.Self); // Rotate around local Y axis
transform.Rotate(Vector3.right, rotationX, Space.Self); // Rotate around local X axis
}
Well, I tried just getting rid of the rolling but it won't satisfy me, because it's a space game (first person).
Can somebody tell me what am I doing wrong? Whenever I tilt my object left or right on Z axis (it has rigidbody) my mouse look is inverted until Z axis is 0 again. Two scripts are seperate but on the same empty object.
My TiltMovement
void TiltMovement()
{
float tiltRight = Input.GetKey(GameManager.Instance.InputManager.GetKey("TiltRight")) ? 1 : Input.GetKey(GameManager.Instance.InputManager.GetKey("TiltLeft")) ? -1 : 0;
if (tiltRight != 0)
{
// Calculate the rotation amount based on the tilt input
float tiltAmount = tiltRight * tiltSpeed * Time.deltaTime;
// Apply Z-axis rotation in local space
transform.Rotate(Vector3.forward, -tiltAmount, Space.Self);
}
My Mouse Look
void HandleMouseLook()
{
float mouseX = Input.GetAxis("Mouse X") * lookSpeed * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * lookSpeed * Time.deltaTime;
targetMouseDelta = new Vector2(mouseX, mouseY);
currentMouseDelta = Vector2.Lerp(currentMouseDelta, targetMouseDelta, smoothFactor * Time.deltaTime);
rotationY += currentMouseDelta.x;
rotationX -= currentMouseDelta.y;
rotationX = Mathf.Clamp(rotationX, -clampAngle, clampAngle);
// Apply rotations in local space
transform.Rotate(Vector3.up, rotationY, Space.Self); // Rotate around local Y axis
transform.Rotate(Vector3.right, rotationX, Space.Self); // Rotate around local X axis
}
Well, I tried just getting rid of the rolling but it won't satisfy me, because it's a space game (first person).
Share Improve this question asked Feb 24 at 12:57 Patryk Patek2 WaraksaPatryk Patek2 Waraksa 211 silver badge1 bronze badge1 Answer
Reset to default 0Rotating on Z (roll) affects the object's local X and Y axes. When you roll (tilt on the Z-axis), your local right (Vector3.right) and up (Vector3.up) directions change. This makes your mouse look behave unexpectedly because it's based on local axes, which are no longer aligned with world space. Mouse look rotates in local space, so when tilted, X and Y behave differently. It is better to use Quaternions instead of separate euler rotations.
Quaternion currentRotation = transform.rotation;
// Apply rotation based on world space (avoiding local axis issues)
Quaternion yRotation = Quaternion.Euler(0, rotationY, 0); // Yaw (left-right)
Quaternion xRotation = Quaternion.Euler(rotationX, 0, 0); // Pitch (up-down)
// Apply new rotation while preserving Z-axis roll
transform.rotation = yRotation * currentRotation * xRotation;
Instead of applying Rotate() separately for each axis, this creates a single new rotation using quaternions. Please reply to this comment for further queries I will try my best to help. And Notify me if the problem has been solved I will be grateful. I apologize in advance if I was unable to understand the scenario to maximum detail.
本文标签: cUnity 3D60 ver Mouse rotation is inverted when TiltRotation is doneStack Overflow
版权声明:本文标题:c# - Unity 3D, 6.0 ver. Mouse rotation is inverted when TiltRotation is done - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741268832a2368927.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论