简体   繁体   English

unity 3D 第一人称机芯

[英]Unity 3D first person movement

I'm following brackeys 1st person movement tutorial.我正在关注 brackeys 第一人称动作教程。 but I can't get the camera working.但我无法让相机工作。 I followed the tutorial correctly, But this code isn't working.我正确地遵循了教程,但是这段代码不起作用。 gives no errors but it doesn't work.没有错误,但它不起作用。 here's the code这是代码

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

public class MouseLook : MonoBehaviour
{
    public float mouseSensitivity = 100f;
    public Transform playerBody;

    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") * mouseSensitivity * Time.deltaTime;
        float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;

        xRotation -= mouseY;
        xRotation = Mathf.Clamp(xRotation, -90f, 90f);
        transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);

        playerBody.Rotate(Vector3.up * mouseX);
    }
}

This code gives no errors but doesn't work.此代码没有错误,但不起作用。 how can i fix this?我怎样才能解决这个问题?

The code you've attached is correct.您附上的代码是正确的。

I recommend you follow the tutorial once more and pay close attention to where this script should be attached.我建议您再次按照本教程进行操作,并密切注意该脚本的附加位置。

Having checked the tutorial myself right now I got it to work by parenting camera as a child of player, attaching MouseLook.cs to Camera game object, and setting player game object as a Player Body variable in MouseLook.cs.I recommend making sure that mouseSensitivity variable is set to 100. Next, if that won't solve your issue, it could mean a number of things.现在我自己检查了教程后,我通过将相机作为玩家的孩子来使用它,将 MouseLook.cs 附加到相机游戏 object,并将玩家游戏 object 设置为 MouseLook.cs 中的玩家身体变量。我建议确保mouseSensitivity变量设置为 100。接下来,如果这不能解决您的问题,它可能意味着很多事情。

First, your project can have Axis "Mouse X" and "Mouse Y" unset, meaning that it does not record the movement of your mouse when you move it.首先,您的项目可以取消设置轴“鼠标 X”和“鼠标 Y”,这意味着它不会在您移动鼠标时记录鼠标的移动。

Second, player or camera rotation can be overridden by some other behavior that might cause MouseLook rotation to be ignored.其次,玩家或摄像机的旋转可能会被其他一些可能导致 MouseLook 旋转被忽略的行为所覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM