简体   繁体   English

我想制作一个鼠标外观脚本,它可以移动我的头部,然后在某一点上移动我的整个身体。 我想不通

[英]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.

相关问题 我想让它当我点击时,屏幕上的子弹移动 - I want to make it so that when I click, the bullet on screen moves 我不知道是什么导致我的程序变慢 - I can't figure out what is slowing my program down 我不知道为什么我的图像不会重绘 - I can't figure out why my image will not repaint 我无法弄清楚我的增量变量放在哪里 - I can't figure out where to put my incrementing variable 我无法突破我的ClipRectangle,我想哭 - I can't break out of my ClipRectangle and I want to cry 当我的角色移动到不同的方向时,我的 Unity 2D 播放器控制器脚本不会使我的角色翻转 - My Unity 2D player controller script won't make my character flip when he moves to a different direction 无论我的鼠标移动如何,如何让我的刚体子弹直线射击? - How can I make my rigid body bullets shoot in a straight line no matter my mouse movement? 当我运行程序时,Visual Studio移动标签 - Visual Studio moves labels when I run my program 我不知道为什么我的斐波那契数列不起作用 - I can't figure out why my fibonacci sequence isn't working 区分用户生成的和我自己的应用程序生成的鼠标移动 - Distinguish between user generated and my own application generated mouse moves
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM