我想制作一个鼠标外观脚本,移动我的头,然后在某个点上移动我的整个 body。我想不通
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
我想创建一个脚本,它使用我的鼠标控制我的相机,就像基本的 MouseLook 脚本一样。但是,我想让相机随着头部移动,当头部到达特定角度时,我想让整个 body 移动......我想不通。我已经使用 Unity 一年多了,从 MONTHS 开始就试图完成它。我想是时候学习更高级的东西了。感谢任何帮助,谢谢!
可能的解决方案:
(我猜想在你的项目中移动相机==移动头部)
你可以得到头 Transform.eulerAngles
和 body Transform.eulerAngles
之间的相对角度,只需使用 Vector3.Angle
(https://docs.unity3d.com/ScriptReference/Vector3.Angle.html)
然后,在 Update
方法中,您可以手动旋转 body,例如 Rigidbody.MoveRotation
(https://docs.unity3d.com/ScriptReference/Rigidbody.MoveRotation.html).
您也可以对这个旋转进行 lerp 处理以使其平滑。
(如果您添加一些代码,我可以更准确地帮助您)
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));
}
}
我想创建一个脚本,它使用我的鼠标控制我的相机,就像基本的 MouseLook 脚本一样。但是,我想让相机随着头部移动,当头部到达特定角度时,我想让整个 body 移动......我想不通。我已经使用 Unity 一年多了,从 MONTHS 开始就试图完成它。我想是时候学习更高级的东西了。感谢任何帮助,谢谢!
可能的解决方案: (我猜想在你的项目中移动相机==移动头部)
你可以得到头 Transform.eulerAngles
和 body Transform.eulerAngles
之间的相对角度,只需使用 Vector3.Angle
(https://docs.unity3d.com/ScriptReference/Vector3.Angle.html)
然后,在 Update
方法中,您可以手动旋转 body,例如 Rigidbody.MoveRotation
(https://docs.unity3d.com/ScriptReference/Rigidbody.MoveRotation.html).
您也可以对这个旋转进行 lerp 处理以使其平滑。 (如果您添加一些代码,我可以更准确地帮助您)
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));
}
}