-1
\$\begingroup\$
using UnityEngine;

public class PlayerMove : MonoBehaviour
{
    public CharacterController controller;
    public Transform trnsform;

    public float JumpH = 3f;

    //these are the speed variables (which are the variables that are the problem)
    public float Speed = 20f;
    public float AirSpeed = 10;

    // this is to check if the player is on the ground
    public LayerMask Groundmask;
    public Transform groundcheck;
    bool isGrounded;
    public float GroundDistance = 0.4f;

    bool isCrouching = false;

    Vector3 Velocity;
    public float Gravity = -9.81f;

    private void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        //this is the movement
        float X = Input.GetAxis("Horizontal");
        float Z = Input.GetAxis("Vertical");
        Vector3 move = transform.right * X + transform.forward * Z;
        Velocity.y += Gravity * Time.deltaTime;
        controller.Move(Velocity * Time.deltaTime);
        controller.Move(move * Speed * Time.deltaTime);

        //this defines what needs to be met for isGrounded to be true
        isGrounded = Physics.CheckSphere(groundcheck.position, GroundDistance, Groundmask);

        //this resets the players downward velocity when they're on the ground
        if(isGrounded == true  && Velocity.y < 0) 
        {
            Velocity.y = -2f;
        }
        
        //this controls jumping
        if(Input.GetButtonDown("Jump") && isGrounded && !isCrouching) 
        {
            Velocity.y = Mathf.Sqrt(JumpH * -2f * Gravity);
        }
        else
        {
            if (Input.GetButtonDown("Jump") && isGrounded)
            {
                Velocity.y = Mathf.Sqrt(1.5f * -2f * Gravity);

            }
        }

        //this is the code that isn't working
        if(isCrouching)
        {
            Speed = 10f;
        }
        else
        {
            Speed = 20f;
        }

        //this makes the player crouch
        if(Input.GetKey("left ctrl")) 
        {
            
            trnsform.localScale = new Vector3(1f, 0.5f, 1f);
            isCrouching = true;
        }
        else 
        {
            trnsform.localScale = new Vector3(1f, 1f, 1f);
            isCrouching = false;
        }

        //this makes the player move slower while in air
        if (isGrounded == false)
        {
            Speed = AirSpeed;
        }
        else
        {
            Speed = 20; 
        }

    }

    //this makes the player bounce when they hit anyting tagged with bounce
    private void OnControllerColliderHit(ControllerColliderHit hit)
    {
        if(hit.collider.tag == "Bounce") 
        {
            Velocity.y = Mathf.Sqrt(9f * -2f * Gravity);
        }
    }

}
 
\$\endgroup\$
0
5
\$\begingroup\$

At the end of your Update method, you have this block:

    if (isGrounded == false)
    {
        Speed = AirSpeed;
    }
    else
    {
        Speed = 20; 
    }

That means that after this block, Speed will be either AirSpeed or 20. The stuff you are doing further above based on the value of isCrouching is irrelevant, because it gets overwritten before the value is used for anything.

So you need to add some logic in the else-branch to decide whether to set the speed to the crouch speed or to the walk speed.

    if (isGrounded == false)
    {
        Speed = AirSpeed;
    }
    else
    {
        if (isCrouching) {
            Speed = CrouchSpeed; 
        } 
        else 
        {
            Speed = WalkSpeed; 
        } 
    }
\$\endgroup\$
1
  • \$\begingroup\$ This works and is the fastest solution to get them going. I recommend, however, considering having things like "in the air" and "crouching" being flags that modify the overall velocity of the character. This will let you have a walk speed, crouch speed, air speed, and crouching air speed. It's harder to work out the formula but knocks out some if-else branches that can get really hairy really fast if you're not careful. \$\endgroup\$
    – James
    Dec 28 '21 at 14:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .