Advertisement
LeeMace

Single Jump w Layer Mask

Aug 31st, 2023
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.93 KB | Gaming | 0 0
  1.  private Rigidbody playerRB;
  2.  
  3.  public float jumpVelocity = 10;
  4.  private bool isJumping;
  5.  
  6.  public float distanceToGround = 0.1f;
  7.  public LayerMask groundLayer;
  8.  private CapsuleCollider playerCollider;
  9.  
  10.  void Start()
  11.  {
  12.      playerRB = GetComponent<Rigidbody>();
  13.      playerCollider = GetComponent<CapsuleCollider>();
  14.  }
  15.  
  16.  void Update() {
  17.  
  18.      isJumping |= Input.GetKeyDown(KeyCode.J);
  19.  }
  20.  
  21.  private void FixedUpdate() {
  22.      
  23.      if (IsGrounded() && isJumping)
  24.      {
  25.          playerRB.AddForce(Vector3.up * jumpVelocity, ForceMode.Impulse);
  26.      }
  27.      isJumping = false;
  28.  }
  29.  
  30.  private bool IsGrounded() {
  31.  
  32.      Vector3 capsuleBottom = new Vector3(playerCollider.bounds.center.x, playerCollider.bounds.min.y, playerCollider.bounds.center.z);
  33.      bool grounded = Physics.CheckCapsule(playerCollider.bounds.center, capsuleBottom, distanceToGround, groundLayer, QueryTriggerInteraction.Ignore);
  34.      return grounded;
  35.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement