Hello! I made this script for double jumping.But my player does not double jump,but it can jump in the air once if it falls off the ground. here’s the script:-
using UnityEngine; using System.Collections;
public class PlayerControls : MonoBehaviour {
- Rigidbody rb;
- public bool IsGrounded ;
- public bool can2j ;
- public float jumpHeight;
- public float jumpSpeed;
- Animator anim;
- void Start(){
- anim = GetComponent ();
- rb = GetComponent ();
- }
- void FixedUpdate ()
- {
- if (IsGrounded) {
- can2j = false;
- }
- if (Input.GetKeyDown (KeyCode.LeftControl)) {
- anim.SetBool (“attack”, true);
- } else {
- anim.SetBool (“attack”, false);
- }
- if (Input.GetKeyDown (KeyCode.Space) && IsGrounded){
-
- rb.velocity = new Vector3 (0, jumpHeight, 0);
- IsGrounded = false;
- can2j = false;
- }
- if(Input.GetButtonDown(“Jump”)) {
- if(!IsGrounded&& !can2j){
- rb.velocity = new Vector3 (0, jumpSpeed, 0);
- can2j = true;
- }
- }
- float moveH = Input.GetAxis (“Horizontal”);
- Vector3 movement = new Vector3 (moveH * 20, rb.velocity.y, 0f);
- rb.velocity = movement;
- if (moveH < 0) {
- rb.rotation = Quaternion.Euler (0, 180, 0);
- }
- if (moveH > 0 && transform.eulerAngles.y == 180) {
-
- rb.rotation = Quaternion.Euler (0, 0, 0);
- }
- }
- void OnCollisionEnter(Collision other){
- if (other.gameObject.tag == “Ground”) {
- IsGrounded = true;
- }
- }
- void OnCollisionExit (Collision other)
- {
- if (other.gameObject.tag == “Ground”) {
- IsGrounded = false;
- }
- }void OnCollisionStay(Collision other){
- if (other.gameObject.tag == “Ground”) {
- IsGrounded = true;
- }
- }
- }