不能统一地看侧面

发布于 2025-02-12 10:41:31 字数 1349 浏览 1 评论 0原文

我想制作FPS游戏。要创建第一人称观众,我遵循本教程 youtube

而且我可以成功地抬头或向下看,但看上去侧面。

这是代码:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class mouselook : MonoBehaviour
{
    float mousesenitivevity = 100f;
    public Transform player_eyes;

    float xRotation = 0f;

    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update()
    {
        float mouseX = Input.GetAxis("Mouse X") * mousesenitivevity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mousesenitivevity * Time.deltaTime;

        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        player_eyes.Rotate(Vector3.up * mouseX);

    }
}

我真的不知道错误发生在哪里,但我注意到了一些事情:

如果您评论行transform.localrotation.localrotation = quaternion.euler(xrotation,0f,0f,0f); << /code>然后,玩家可以向侧面看,但不能上下看,如果您输入播放器,玩家可以上下查找,但不能侧面。

这是Unity编辑器屏幕截图以获取更多详细信息: 编辑器image

I want to make a fps game. To create first person viewer I followed this tutorial youtube

I followed everything that he told to do and I could successfully look up or down but could not look sideways.

Here`s the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class mouselook : MonoBehaviour
{
    float mousesenitivevity = 100f;
    public Transform player_eyes;

    float xRotation = 0f;

    // Start is called before the first frame update
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    // Update is called once per frame
    void Update()
    {
        float mouseX = Input.GetAxis("Mouse X") * mousesenitivevity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mousesenitivevity * Time.deltaTime;

        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);

        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
        player_eyes.Rotate(Vector3.up * mouseX);

    }
}

I really do not know where the error is occurring but here are a few things I noticed:

If you comment out the line transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f); then the player can look sideways but not up and down and if you uncomment it the player can look up and down but not sideways.

Here is the unity editor screenshots for more details:
Editor image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

战皆罪 2025-02-19 10:41:31

解决了,我错误地将脚本放在播放器上,而不是相机上。

Solved, I put the script on the player instead of the camera by mistake.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文