Problem with Jumping code in Unity 2D,problem with Jumping code in Unity 2D
So this is my first game in unity, I've been following a tutorial by "Let's Make a Game Together" but I have a problem with the jumping code it seems I can double jump not too sure why here's the code. Thanks in advance for any and all answers :)
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class PlayerMoveProt : MonoBehaviour {
public int playerSpeed = 10; public int playerJumpPower = 2250; private float moveX; public bool isGrounded = true; public float distanceToBottomOFPLayer = 0.7f;
// Update is called once per frame void FixedUpdate(){
PlayerMove(); PlayerRaycast();
}
void PlayerMove() { //CONTROLS moveX = Input.GetAxis("Horizontal"); if (Input.GetButtonDown("Jump") && isGrounded == true){ Jump(); } //ANIMATION if (moveX != 0) { GetComponent().SetBool("isRunning", true); } else { GetComponent().SetBool("isRunning", false); } //PLAYER DIRECTION if (moveX < 0.0f){
GetComponent().flipX = true; } else if (moveX > 0.0f){ GetComponent().flipX = false; } //PHYSICS gameObject.GetComponent().velocity = new Vector2(moveX * playerSpeed, gameObject.GetComponent().velocity.y); }
void Jump() { //JUMPING CODE GetComponent().AddForce(Vector2.up * playerJumpPower); isGrounded = false; }
void PlayerRaycast(){ //Ray Up RaycastHit2D rayUp = Physics2D.Raycast(transform.position, Vector2.up); if (rayUp != null && rayUp.collider != null && rayUp.distance < distanceToBottomOFPLayer && rayUp.collider.name == "Box2"){ Destroy(rayUp.collider.gameObject);
} //Ray Down RaycastHit2D rayDown = Physics2D.Raycast(transform.position, Vector2.down); if (rayDown != null && rayDown.collider != null && rayDown.distance < distanceToBottomOFPLayer && rayDown.collider.tag == "enemy"){ GetComponent().AddForce(Vector2.up * playerJumpPower); rayDown.collider.gameObject.GetComponent().enabled = false; rayDown.collider.gameObject.GetComponent().enabled = false; } isGrounded = false; if (rayDown != null && rayDown.collider != null && rayDown.distance < distanceToBottomOFPLayer && rayDown.collider.tag != "enemy"){ isGrounded = true; } } }