Home > Net >  Player moves at a very slow speed in Unity
Player moves at a very slow speed in Unity

Time:01-06

I'm making a first-person movement script for the player, and everything movement-wise seems to work fine (other than an unrelated issue of the player not moving in the camera's direction), however when running in the game, the player moves at a very slow speed.

Here's the movement script:

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

public class PlayerMovement : MonoBehaviour
{
    private Rigidbody RB;
    [SerializeField] private GameObject cam;

    private float walkVel; // Forward & backward movement
    private float strafeVel; // Sideways movement
    [SerializeField] public float walkSpeed = 5f;

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

    private void Update()
    {
        walkVel = Input.GetAxis("Vertical");
        strafeVel = Input.GetAxis("Horizontal");
    }

    private void FixedUpdate()
    {
        transform.localRotation = Quaternion.Euler(0f, cam.transform.rotation.y * Time.deltaTime, 0f);

        Vector3 Up = new Vector3(RB.velocity.x, RB.velocity.y, RB.velocity.z);
        RB.velocity = (transform.forward * strafeVel   transform.right * walkVel).normalized * walkSpeed * Time.deltaTime;
    }
}

I'm pretty sure the issue is not related to the camera script, but here it is:

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

public class PlayerCamPositioning : MonoBehaviour
{
    private Vector2 camTurn;

    [SerializeField] public float camSensitivity = 5f;

    private void Start()
    {
        Cursor.lockState = CursorLockMode.Locked;
    }

    private void Update()
    {
        camTurn.x  = Input.GetAxis("Mouse X") * camSensitivity;
        camTurn.y -= Input.GetAxis("Mouse Y") * camSensitivity;
        transform.localRotation = Quaternion.Euler(camTurn.y, camTurn.x, 0);
    }
}

I tried to implement a very basic velocity script, and alternatively RB.AddVelocity to the Rigidbody, however it did not solve my problem.

I also tried simply increasing the walkSpeed variable to increase player speed, which actually solves my problem, HOWEVER I want to refrain from using this solution as previously the player moved at a relatively normal speed with walkSpeed set to 5f.

CodePudding user response:

All I had to do to solve my problem was remove Time.deltaTime from the RB.Velocity vector. The vector already runs in the FixedUpdate function, so I think it should work as intended now.

CodePudding user response:

Sounds like you just need to tweak your values. Try splitting your equation into pieces and logging the individual and multiplied value so you can understand what values you see versus what values you're expecting.

I see many variables in your code that can be less than 1. strafeVel, walkVel and Time.deltaTime. In fact, in my experience, Time.deltaTime is always a very small float.

Perhaps you would prefer to use RB.velocity with = or *= along with a Mathf.Clamp() to gradually increase your speed.

  • Related