Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Player walks faster while pressing forward/back and any of the side keys

Discussion in 'Scripting' started by chef_chicken, Jun 4, 2023.

  1. chef_chicken

    chef_chicken

    Joined:
    Oct 30, 2021
    Posts:
    5
    I'm working on a player controller using the character controller component. But, when I hold forward/back and any of the side keys, the player will walk in 2x speed. I have tried to clamp the magnitude. But, it still walks 2x faster.

    Code (CSharp):
    1.     CharacterController cc;
    2.     public Transform cam;
    3.     public float sens;
    4.     public float speed;
    5.  
    6.     float xro;
    7.  
    8.     private void Start()
    9.     {
    10.         cc = GetComponent<CharacterController>();
    11.  
    12.         Cursor.lockState = CursorLockMode.Locked;
    13.     }
    14.  
    15.     private void Update()
    16.     {
    17.         Cursor.visible = true;
    18.  
    19.         float cam_x = Input.GetAxis("Mouse X") * sens * Time.deltaTime;
    20.         float cam_y = Input.GetAxis("Mouse Y") * sens * Time.deltaTime;
    21.  
    22.         float move_x = Input.GetAxis("Horizontal") * speed * Time.deltaTime;
    23.         float move_z = Input.GetAxis("Vertical") * speed * Time.deltaTime;
    24.  
    25.         xro -= cam_y;
    26.         xro = Mathf.Clamp(xro, -90, 90);
    27.  
    28.         transform.Rotate(0f, cam_x, 0f);
    29.         cam.transform.localRotation = Quaternion.Euler(xro, 0f, 0f);
    30.  
    31.  
    32.  
    33.         Vector3 movement = new Vector3(move_x, 0, move_z);
    34.         movement = Vector3.ClampMagnitude(movement, speed);
    35.  
    36.         cc.Move(transform.forward * movement.z + transform.right * movement.x);
    37.     }
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,882
    You will just want to normalise your input vector before you use it further on. You can also combine it into one Vector3 and then multiply it by speed, etc (after you normalize it), to avoid repeating code.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
  4. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    272
    You are multiplying input by speed * DeltaTime, then Clamping magnitude by speed.
    What you should do is clamping input by 1, only after that multiplying by speed * DeltaTime.

    By multiplying it with DeltaTime you are making it too low it's never been clamped by speed in the first place.
     
  5. chef_chicken

    chef_chicken

    Joined:
    Oct 30, 2021
    Posts:
    5
    Oohhh, ok. Thank you for the help.
     
  6. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    340
    To be more precise, it's 1.41421356237 faster.