Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Player moves at a very slow speed in Unity

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading