Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Bug Crouch speed is faster than Movement Speed

Discussion in 'Scripting' started by teodorking1018, Jun 26, 2021.

  1. teodorking1018

    teodorking1018

    Joined:
    Jun 26, 2021
    Posts:
    16
    In short, when I click CTRL my player goes down but it goes faster than when it moves normally, why?

    Code (CSharp):
    1.   public float movementSpeed = 10.0f;
    2.     public float crouchSpeed = 2.0f;
    3. ///////////////////////////////////////////////////////
    4. if (Input.GetKey(KeyCode.LeftControl))
    5.         {
    6.             isCrouching = true;
    7.         }
    8. ///////////////////////////////////////////////////////
    9.  
    10. if (isCrouching == true)
    11.         {
    12.             controller.height = crouchingHeight;
    13.             float curSpeedX = canMove ? (isCrouching ? crouchSpeed : movementSpeed) * Input.GetAxis("Vertical") : 0;
    14.             float curSpeedY = canMove ? (isCrouching ? crouchSpeed : movementSpeed) * Input.GetAxis("Horizontal") : 0;
    15.             movement *= crouchingMulitplier;
    16.             float movementY = movement.y;
    17.             movement = (transform.forward * curSpeedX) + (transform.right * curSpeedY);
    18.         }```
     
  2. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    350
    What's crouching multiplier?
    Have you tried to debug to check if curSpeedX/Y is bigger or lower when crouching? It's probably wrong calculations from your code. Debug and check exact values assigned every frame.
     
  3. teodorking1018

    teodorking1018

    Joined:
    Jun 26, 2021
    Posts:
    16
    Code (CSharp):
    1.     public bool isCrouching = false;
    2.     public float crouchingMulitplier;
    I also tried to beg for the movement of the *= squatMulitplier;, but it's the same again
     
  4. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    350
    Again, check exact values. If you are moving with controller.Move(), you can log exact vector every frame and see, is it bigger with crouch.
     
  5. teodorking1018

    teodorking1018

    Joined:
    Jun 26, 2021
    Posts:
    16
    I'm new to this, could you explain to me how to do it? PLS
     
  6. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    350
    Go to place when you have controller.Move(). Write this line.
    Code (CSharp):
    1. Debug.Log("Movement " + movement);
    2. controller.Move(movement);
    Check if it's bigger with crouch. 100% it's, so it's probably some calculations problems.
     
  7. teodorking1018

    teodorking1018

    Joined:
    Jun 26, 2021
    Posts:
    16
    He tells me that when I walk it's normal speed = 1, and when crouch speed is 2 ( normal). Normal speed should be 5, not 1.
    What could it be up to?
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,645
    If something is public:

    The instant you add that to your scene / prefab it is 10f ... AND THEN IT GETS SERIALIZED.

    Now go change the value in code. Note that it has no effect: the serialized value writes over the code value.

    This is how Unity serialization works.

    If you want to define it in code, make the variable private.

    If you want to define it in the editor, make it public and DO NOT INITIALIZE IT.

    Instead, make a Reset function, which ONLY runs at the instant it is added:

    Code (csharp):
    1. void Reset()
    2. {
    3.   movementSpeed = 10.0f;
    4. }
    And
    Reset()
    only runs in editor when you drag it on there.

    MORE: https://forum.unity.com/threads/sensitivity-in-my-mouselook-script.1061612/#post-6858908
     
  9. teodorking1018

    teodorking1018

    Joined:
    Jun 26, 2021
    Posts:
    16