[英]I want to make a mouse look script, that moves my head, then at a point, moves my whole body. I can't figure it out
I want to create a script, which controls my camera using my mouse, like a basic MouseLook script.我想创建一个脚本,它使用我的鼠标控制我的相机,就像一个基本的 MouseLook 脚本。 But, I want the camera to move with the head, and when the head reaches a specific angle, I want the whole body to move... I can't figure it out.但是,我想让相机随着头部移动,当头部到达特定角度时,我想让整个身体移动……我想不通。 I have been using Unity for more then a year now, trying to finish this since MONTHS.我已经使用 Unity 一年多了,从 MONTHS 开始就试图完成它。 I think it's about time I learn something more advanced.我想是时候学习更高级的东西了。 Any help appreciated, thank you!任何帮助表示赞赏,谢谢!
Possible solution: (I guess that moving the camera == moving the head in your project)可能的解决方案:(我猜在你的项目中移动相机 == 移动头部)
You can get relative angle between head Transform.eulerAngles
and body Transform.eulerAngles
just using Vector3.Angle
( https://docs.unity3d.com/ScriptReference/Vector3.Angle.html ) Then, in the Update
method you can rotate your body manually, for example, Rigidbody.MoveRotation
( https://docs.unity3d.com/ScriptReference/Rigidbody.MoveRotation.html ).您可以使用Vector3.Angle
( https://docs.unity3d.com/ScriptReference/Vector3.Angle.html ) 获得头部Transform.eulerAngles
和身体Transform.eulerAngles
之间的相对角度然后,在Update
方法中,您可以旋转身体手动,例如Rigidbody.MoveRotation
( https://docs.unity3d.com/ScriptReference/Rigidbody.MoveRotation.html )。
You can also lerp this rotation to make it smoothly.您还可以调整此旋转以使其平滑。 (I can help you more presizeliy if you add some code) (如果您添加一些代码,我可以为您提供更多帮助)
And 7 months later, I'M BACK! 7 个月后,我回来了!
public GameObject playerHead;
public GameObject character;
void Update()
{
//check if the head is at a specific angle and if the mouse is moving
if (playerHead.transform.localRotation.x >= 0.59f && Input.GetAxis("Mouse X") < 0) {
//rotate the body at the speed of the mouse
transform.Rotate(new Vector3(0, Input.GetAxis("Mouse X"), 0));
//repeat
} else if (playerHead.transform.localRotation.x <= -0.59f && Input.GetAxis("Mouse X") > 0) {
transform.Rotate(new Vector3(0, Input.GetAxis("Mouse X"), 0));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.