Question by
MiloRoban · Apr 09, 2018 at 02:00 AM ·
2d gameplatformer
How to make Player jump only once when in the air?
Hello, so I am creating a 2D endless platformer and I need to make it so the player can only jump while on the ground.
Here is my code. using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
private float moveSpeedStore;
public float speedMultiplier;
public float speedIncreaseMilestone;
private float speedIncreaseMilestoneStore;
private float speedMilestoneCount;
private float speedMilestoneCountStore;
public float jumpForce;
public float jumpTime;
private float jumpTimeCounter;
private bool stoppedJumping;
private bool canDoubleJump;
private Rigidbody2D myRigidbody;
public bool grounded;
public LayerMask whatIsGround;
public Transform groundCheck;
public float groundCheckRadius;
//private Collider2D myCollider;
private Animator myAnimator;
public GameManager theGameManager;
public AudioSource jumpSound;
public AudioSource deathSound;
// Use this for initialization
void Start()
{
myRigidbody = GetComponent<Rigidbody2D>();
//myCollider = GetComponent<Collider2D>();
myAnimator = GetComponent<Animator>();
jumpTimeCounter = jumpTime;
speedMilestoneCount = speedIncreaseMilestone;
moveSpeedStore = moveSpeed;
speedMilestoneCountStore = speedMilestoneCount;
speedIncreaseMilestoneStore = speedIncreaseMilestone;
stoppedJumping = true;
}
// Update is called once per frame
void Update()
{
//grounded = Physics2D.IsTouchingLayers(myCollider, whatIsGround);
grounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);
if (transform.position.x > speedMilestoneCount)
{
speedMilestoneCount += speedIncreaseMilestone;
speedIncreaseMilestone = speedIncreaseMilestone * speedMultiplier;
moveSpeed = moveSpeed * speedMultiplier;
}
myRigidbody.velocity = new Vector2(moveSpeed, myRigidbody.velocity.y);
if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0))
{
if (grounded)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
stoppedJumping = false;
jumpSound.Play();
}
if (!grounded && canDoubleJump)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
jumpTimeCounter = jumpTime;
stoppedJumping = false;
canDoubleJump = false;
jumpSound.Play();
}
}
if ((Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0)) && !stoppedJumping)
{
if (jumpTimeCounter > 0)
{
myRigidbody.velocity = new Vector2(myRigidbody.velocity.x, jumpForce);
jumpTimeCounter -= Time.deltaTime;
}
}
if (Input.GetKeyUp(KeyCode.Space) || Input.GetMouseButtonUp(0))
{
jumpTimeCounter = 0;
stoppedJumping = true;
}
if (grounded)
{
jumpTimeCounter = jumpTime;
canDoubleJump = true;
}
myAnimator.SetFloat("Speed", myRigidbody.velocity.x);
myAnimator.SetBool("Grounded", grounded);
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "killbox")
{
theGameManager.RestartGame();
moveSpeed = moveSpeedStore;
speedMilestoneCount = speedMilestoneCountStore;
speedIncreaseMilestone = speedIncreaseMilestoneStore;
deathSound.Play();
}
}
}
Comment
Your answer
Follow this Question
Related Questions
my character is sitting in the middle of my platform during game play 0 Answers
Having troubles with the checkpoints opening on my 2D platformer and its making my sprite dissapear. 1 Answer
Problems with player jumping (Welcome to Unity Answers The best place to ask and answer questions ) 0 Answers
Help! Create interactive button with an image + c# Script 0 Answers
Trying to get bool statement to work in OnTriggerStay2D 1 Answer