Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private Rigidbody playerRB;
- public float jumpVelocity = 10;
- private bool isJumping;
- public float distanceToGround = 0.1f;
- public LayerMask groundLayer;
- private CapsuleCollider playerCollider;
- void Start()
- {
- playerRB = GetComponent<Rigidbody>();
- playerCollider = GetComponent<CapsuleCollider>();
- }
- void Update() {
- isJumping |= Input.GetKeyDown(KeyCode.J);
- }
- private void FixedUpdate() {
- if (IsGrounded() && isJumping)
- {
- playerRB.AddForce(Vector3.up * jumpVelocity, ForceMode.Impulse);
- }
- isJumping = false;
- }
- private bool IsGrounded() {
- Vector3 capsuleBottom = new Vector3(playerCollider.bounds.center.x, playerCollider.bounds.min.y, playerCollider.bounds.center.z);
- bool grounded = Physics.CheckCapsule(playerCollider.bounds.center, capsuleBottom, distanceToGround, groundLayer, QueryTriggerInteraction.Ignore);
- return grounded;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement