不能同时旋​​转和移动

bar*_*ecu 5 c# unity-game-engine game-physics

我目前正在 Unity 3D 中开发一款 FPS 射击游戏。我对 Unity 还很陌生,而且我的玩家移动脚本遇到了一些问题。单独来看,一切似乎都正常,我可以自由旋转和移动播放器,但是当我尝试同时执行这两个操作时,我的播放器似乎锁定并且不会旋转。

有时跳跃或移动到地形上的较高地面似乎可以解决问题,但是我在禁用重力、没有碰撞器且玩家位于地面上方的情况下进行了一些检查,因此问题似乎出在脚本上。我还做了一些调试,Rotate() 代码确实运行了,只是旋转量似乎没有改变。

这是玩家移动脚本:

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

[RequireComponent(typeof(Rigidbody))]
public class PlayerMov : MonoBehaviour
{
    [SerializeField]
    private Camera cam;
    [SerializeField]
    private float speed = 5f;
    [SerializeField]
    private float looksensitivity = 4f;

    [Header("Camera View Lock:")]
    [SerializeField] //min and max amount for looking up and down (degrees)
    private float lowlock = 70f;
    [SerializeField]
    private float highlock = 85f;

    private Rigidbody rb;
    private float currentrotx; //used later for calculating relative rotation
    public Vector3 velocity;
    public Vector3 rotation; // rotates the player from side to side
    public float camrotation; // rotates the camera up and down
    public float jmpspe = 2000f;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        CalcMovement();

        CalcRotation();

        Rotate();
    }

    void FixedUpdate()
    {
        Move();
    }

    private void CalcRotation()
    {
        float yrot = Input.GetAxisRaw("Mouse X"); //around x axis
        rotation = new Vector3(0f, yrot, 0f) * looksensitivity;

        float xrot = Input.GetAxisRaw("Mouse Y"); //around y axis
        camrotation = xrot * looksensitivity;
    }

    private void CalcMovement()
    {
        float xmov = Input.GetAxisRaw("Horizontal");
        float zmov = Input.GetAxisRaw("Vertical");

        Vector3 movhor = transform.right * xmov;
        Vector3 movver = transform.forward * zmov;

        velocity = (movhor + movver).normalized * speed;
    }

    void Move()
    {
        //move
        if (velocity != Vector3.zero)
        {
            rb.MovePosition(rb.position + velocity * Time.fixedDeltaTime);
        }

        //jump
        if (Input.GetKeyDown(KeyCode.Space))
        {
           //add double jump limit later!   
            rb.AddForce(0, jmpspe * Time.deltaTime, 0, ForceMode.Impulse);

        }
    }

    void Rotate()
    {
        //looking side to side
        rb.MoveRotation(rb.rotation * Quaternion.Euler(rotation));

       // camera looking up and down
        currentrotx -= camrotation;
        currentrotx = Mathf.Clamp(currentrotx, -lowlock, highlock);
        cam.transform.localEulerAngles = new Vector3(currentrotx, 0, 0);
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是我的播放器附带的相关组件:

在此输入图像描述

(播放器还附加了几个组件,但我在没有它们的情况下进行了测试,问题仍然出现)

就像我说的,我是一个团结新手,所以我确信我错过了一些小东西,但我似乎无法将手指放在它上面,我已经坚持了一段时间,所以任何帮助都是非常感激。

bar*_*ecu 4

解决了:

我遇到的问题似乎是因为我是从笔记本电脑运行场景,而不是桌面,我认为这是 Unity 输入的用途。