简体   繁体   中英

Can't rotate and move at the same time

I'm currently developing an FPS shooter in Unity 3D. I'm fairly new to Unity and I've been having a bit of trouble with my player movement script. Individually everything seems to work, I can rotate and move the player freely, however when I try doing the two simultaneously my player seems to lock and won't rotate.

Sometimes jumping or moving to higher ground on the terrain seems to fix the issue, however I ran a few checks with gravity disabled, no colliders, and with the player well above ground, so the problem seems to be with the script. I've also done a bit of debugging and the Rotate() code does run, just the rotation amount doesn't seem to change.

Here is the player movement script:

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);
    }
}

Here are the relevant components attached to my player:

在此处输入图片说明

(The player has a couple more components attached but I ran tests without them and the problem still occurs)

Like I said I'm a bit of a unity novice, so i'm sure I missed something small but I just can't seem to place my finger on it, I've been stuck on this for a while so any help is much appreciated.

SOLVED:

It seems the problem I had was because I was running the scene from my laptop and not a desktop which I assume is what the Unity input was built for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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