Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Player speed isn't changing when crouching but is when sprinting

Discussion in 'Scripting' started by X-TRI3, May 28, 2022.

  1. X-TRI3

    X-TRI3

    Joined:
    Mar 29, 2022
    Posts:
    19
    Hello, I am making a character controller for my 3d game, however, I have an if statement that asks if the player is pressing the sprint button, and it works, and I did the same for crouching. The thing that isn't working is that when I press the sprint button, it does change the speed up, but when I crouch, it does not. Here is the code:
    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlayerMovement : MonoBehaviour
    7. {
    8.  
    9.     public CharacterController controller;
    10.     public float defaultSpeed = 8f;
    11.     public float speed = 8f;
    12.     public float sprintSpeed = 12f;
    13.     public float crouchSpeed = 3f;
    14.     public float gravity = -9.81f;
    15.     public float jumpHeight = 3f;
    16.  
    17.     public Transform groundCheck;
    18.     public float groundDistance = 0.4f;
    19.     public LayerMask groundMask;
    20.     Vector3 velocity;
    21.     bool isGrounded;
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.         isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
    27.  
    28.         if (isGrounded && velocity.y < 0)
    29.         {
    30.             velocity.y = -2f;
    31.         }
    32.  
    33.         float x = Input.GetAxis("Horizontal");
    34.         float z = Input.GetAxis("Vertical");
    35.  
    36.         Vector3 move = transform.right * x + transform.forward * z;
    37.  
    38.         controller.Move(move * speed * Time.deltaTime);
    39.  
    40.         if (Input.GetButtonDown("Jump") && isGrounded)
    41.         {
    42.             velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
    43.         }
    44.        
    45.         if(Input.GetButton("Sprint"))
    46.         {
    47.             speed = sprintSpeed;
    48.         }
    49.         else
    50.         {
    51.             speed = defaultSpeed;
    52.         }
    53.  
    54.         if (Input.GetButton("Crouch"))
    55.         {
    56.             transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
    57.             speed = crouchSpeed;
    58.            
    59.             if (Input.GetButton("Sprint") && Input.GetButton("Crouch"))
    60.             {
    61.                 transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
    62.                 speed = 6f;
    63.             }
    64.             else
    65.             {
    66.                 transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
    67.                 speed = crouchSpeed;
    68.             }
    69.         }
    70.         else
    71.         {
    72.             transform.localScale = new Vector3(1f, 1f, 1f);
    73.             speed = defaultSpeed;
    74.         }
    75.  
    76.         velocity.y += gravity * Time.deltaTime;
    77.  
    78.         controller.Move(velocity * Time.deltaTime);
    79.  
    80.        
    81.     }
    82. }
    83.  
    Thank you in advance.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,517
    Here is how to debug a problem like this:

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494

    You must find a way to get the information you need in order to reason about what the problem is.

    If you want to see another example, here's a fully-finished prototype controller:

    https://forum.unity.com/threads/a-basic-first-person-character-controller-for-prototyping.1169491/

    That one has run, walk, jump, slide, crouch... it's crazy-nutty!!
     
  3. X-TRI3

    X-TRI3

    Joined:
    Mar 29, 2022
    Posts:
    19
    I tried the debug.log and it shows that it is receiving the input, but it is still not changing the speed. I tried to set the speed as a straight up number rather than the variables number and it still did not work.
     
  4. SourGummi

    SourGummi

    Joined:
    Nov 14, 2021
    Posts:
    96
    first, you have a double if loop.
    youre checking if crouch is down then again if crouch is down and sprint is down. you dont need to check again if youre still within the scope of the first check.
    Code (CSharp):
    1. if (Input.GetButton("Crouch"))
    2.         {
    3.             transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
    4.             speed = crouchSpeed;
    5.          
    6.             if (Input.GetButton("Sprint") && Input.GetButton("Crouch"))
    7.             {
    8.                 transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
    9.                 speed = 6f;
    10.             }
    11.             else
    12.             {
    13.                 transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
    14.                 speed = crouchSpeed;
    15.             }
    16.         }
     
  5. SourGummi

    SourGummi

    Joined:
    Nov 14, 2021
    Posts:
    96
    secondly, another doubled function
    Code (CSharp):
    1. transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
    2. speed = crouchSpeed;
    3.    
    4. else
    5. {
    6.        transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
    7.        speed = crouchSpeed;
    8. }
    remove the speed and vector change before the second if statement and remove the double statement part.
     
  6. X-TRI3

    X-TRI3

    Joined:
    Mar 29, 2022
    Posts:
    19
    I changed it and took it out then turned it into a slide mechanic so if sprinting and crouching then slide, and now the only thing that works is slide. It seems like the bottom-most if statement is the only one that actually changes anything even though debug.log says the other ones are capturing the input.
     
  7. SourGummi

    SourGummi

    Joined:
    Nov 14, 2021
    Posts:
    96
    try
    Code (CSharp):
    1.         if (Input.GetButton("Crouch"))
    2.         {
    3.             if (Input.GetButton("Sprint"))
    4.             {
    5.                 transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
    6.                 speed = 6f;
    7.             }
    8.             else
    9.             {
    10.                 transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
    11.                 speed = crouchSpeed;
    12.             }
    13.         }
     
  8. X-TRI3

    X-TRI3

    Joined:
    Mar 29, 2022
    Posts:
    19
    I meant I took the nested if statement out and turned it into this:
    Code (CSharp):
    1. if(Input.GetButton("Vertical") && Input.GetButton("Sprint") && Input.GetButton("Crouch"))
    2.         {
    3.             Debug.Log("Sliding");
    4.             transform.localScale = Vector3.Lerp (transform.localScale, slideScale, scaleSpeed * Time.deltaTime);
    5.             speed = slideSpeed;
    6.         }
    7.         else
    8.         {
    9.             transform.localScale = Vector3.Lerp (transform.localScale, standScale, scaleSpeed * Time.deltaTime);
    10.             speed = defaultSpeed;
    11.         }
     
  9. SourGummi

    SourGummi

    Joined:
    Nov 14, 2021
    Posts:
    96
    still, not a fan of this
    Code (CSharp):
    1. Input.GetButton("Vertical") && Input.GetButton("Sprint") && Input.GetButton("Crouch"))
    have a flag for is_crouched, is_sprinting
    Code (CSharp):
    1. bool is_crouched = false, is_sprinting = false;
    2.  
    3. // moving
    4. if (Input.GetButton("Vertical"))
    5. {
    6.     // sliding
    7.     if (is_crouched && is_sprinting)
    8.         // sliding
    9.         DoSlide();
    10.  
    11.     else if (is_crouched && !is_sprinting)
    12.         // crouch walking
    13.         DoCrouchWalk();
    14.  
    15.     else if (is_sprinting && !is_crouched)
    16.         // just sprint
    17.         DoSprint();
    18.  
    19. }
    20.  
    messy but..
     
  10. X-TRI3

    X-TRI3

    Joined:
    Mar 29, 2022
    Posts:
    19
    This works, thank you.
     
    SourGummi likes this.