Search Unity

Question First-Person Controller Crouching and Speed Issues

Discussion in 'Visual Scripting' started by BubbleGobbler, Nov 5, 2022.

  1. BubbleGobbler

    BubbleGobbler

    Joined:
    Aug 23, 2022
    Posts:
    7
    Hello, game developers. I'm an amateur who is working on a first-person controller, and I've stumbled across two issues that have been bothering me for days. I want to make an fps game and am starting off by creating the player character. The movement is mostly completed as the player can walk, jump, and sprint. The last step is adding crouching. My goal is to make the player crouch on the press of the C key, reducing their walking speed and preventing them from running or jumping while crouching. When they pressed C again in that state, their movement and height were set to default states. I tried to implement this mechanic in my code, but it has not been successful. None of the features that I mentioned worked.
    The second problem is that I cannot change the player's default walking speed. I initially set it to 12 but wanted it to be 10, so I adjusted the variable for the walking speed in my code. I then hit play, but the game changes the variable (the variable is called, "speed") back to 12.
    Once again, thank you so much for your help. If you want more details, feel free to ask.


    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.     [SerializeField]
    9.     private CharacterController character_Controller;
    10.  
    11.  
    12.     Vector3 gravitationalforce;
    13.  
    14.     public float speed = 10;
    15.     float gravity = -9.87f;
    16.     public bool canRun = false;
    17.     public bool canJump = true;
    18.     public bool groundedPlayer;
    19.     public bool crouching = false;
    20.  
    21.  
    22.     // Start is called before the first frame update
    23.     void Start()
    24.     {
    25.         character_Controller = GetComponent<CharacterController>();
    26.         StartCoroutine(Running());
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.         PlayerWalk();
    33.         PlayerGravity();
    34.         PlayerRunning();
    35.         PlayerJumping();
    36.         PlayerCrouching();
    37.     }
    38.  
    39.     public void PlayerWalk()
    40.     {
    41.         float x = Input.GetAxis("Horizontal");
    42.         float z = Input.GetAxis("Vertical");
    43.         Vector3 move = transform.right * x + transform.forward * z;
    44.         character_Controller.Move(move * speed * Time.deltaTime);
    45.  
    46.         if (speed > 14)
    47.         {
    48.             speed = 14f;
    49.         }
    50.     }
    51.  
    52.     public void PlayerGravity()
    53.     {
    54.         groundedPlayer = character_Controller.isGrounded;
    55.         gravitationalforce.y += gravity * Time.deltaTime;
    56.         character_Controller.Move(gravitationalforce * Time.deltaTime);
    57.  
    58.         if (groundedPlayer)
    59.         {
    60.             gravitationalforce.y = 0;
    61.         }
    62.     }
    63.  
    64.    public void PlayerJumping()
    65.     {
    66.         if (groundedPlayer && Input.GetKey(KeyCode.Space))
    67.         {
    68.             gravitationalforce.y += Mathf.Sqrt(1 * -1.5f * gravity);
    69.         }
    70.     }
    71.  
    72.  
    73.     public void PlayerRunning()
    74.     {
    75.         if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey(KeyCode.W))
    76.         {
    77.             canRun = true;
    78.         }
    79.         else
    80.         {
    81.             canRun = false;
    82.         }
    83.  
    84.         if (!groundedPlayer)
    85.         {
    86.             canRun = false;
    87.         }
    88.      
    89.         if (canRun == false)
    90.         {
    91.             speed = 12;
    92.         }
    93.     }
    94.  
    95.  
    96.     IEnumerator Running()
    97.     {
    98.         {
    99.             while (true)
    100.             {
    101.                 if (canRun == true)
    102.                 speed += 1;
    103.                 yield return new WaitForSeconds(0.2f);
    104.             }
    105.         }
    106.     }
    107.  
    108.     public void PlayerCrouching()
    109.     {
    110.         if (Input.GetKeyDown(KeyCode.C) && groundedPlayer)
    111.         {
    112.             crouching = true;
    113.             canJump = false;
    114.             canRun = false;
    115.             speed = 5f;
    116.             character_Controller.height = 0.8f;
    117.         }
    118.  
    119.         if (Input.GetKeyDown(KeyCode.C) && crouching == true)
    120.         {
    121.             crouching = false;
    122.             canJump = true;
    123.             canRun = true;
    124.             speed = 12f;
    125.             character_Controller.height = 2f;
    126.         }
    127.     }
    128. }
     
  2. BubbleGobbler

    BubbleGobbler

    Joined:
    Aug 23, 2022
    Posts:
    7
    I should note that I am completely fine if you give a solution to one issue and not the other. I'll take any help I can get. Thank you!