- Home /
This question was
closed Feb 02, 2019 at 06:25 PM by
floobawkr for the following reason:
I'll be honest I know nothing about programming. I just copied it from a youtuber.
Question by
floobawkr · Feb 02, 2019 at 02:31 PM ·
charactercontroller
how do i make player controller stop jumping infinite times?
public class playercontroller : MonoBehaviour
{
public float Speed;
public float JumpForce;
private float moveInput;
private Rigidbody2D rb;
private bool facingRight = true;
private bool isGrounded;
public Transform groundcheck;
public float checkRadius;
public LayerMask whatIsGround;
private int extraJumps;
public int extraJumpsValue;
void Start(){
extraJumps = extraJumpsValue;
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate() {
isGrounded = Physics2D.OverlapCircle(groundcheck.position, checkRadius, whatIsGround);
moveInput = Input.GetAxis("Horizontal");
Debug.Log(moveInput);
rb.velocity = new Vector2(moveInput * Speed, rb.velocity.y);
if (facingRight == false && moveInput > 0) {
Flip();
} else if (facingRight == true && moveInput < 0){
Flip();
}
}
void Update(){
if(isGrounded == true){
extraJumps = extraJumpsValue;}
if(Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0){
rb.velocity = Vector2.up * JumpForce;
}
else if(Input.GetKeyDown(KeyCode.UpArrow) && extraJumps == 0 && isGrounded == true){
rb.velocity = Vector2.up * JumpForce;
}
}
void Flip() {
if (isGrounded == true)
extraJumps = 0;
if (Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0){
rb.velocity = Vector2.up * JumpForce;
extraJumps --;
}
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}
Comment
Why are you checking for jumps in your flip function? Subtract from extraJumps when you jump and set extraJumps back to extraJumpsValue when you're grounded and extraJumps is less than extraJumpsValue
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.UpArrow) && extraJumps > 0){
rb.velocity = Vector2.up * JumpForce;
extraJumps--;
}
if(grounded && extraJumps < extraJumpsValue)
extraJumps = extraJumpsValue;
Answer by SloppeD · Feb 02, 2019 at 03:52 PM
You have to subtract 1 from extraJumps whenever you jump.
if(Input.GetKeyDown(KeyCode.UpArrow) && extraJumps > 0){
rb.velocity = Vector2.up * JumpForce;
extraJumps = extraJumps - 1;
}
Follow this Question
Related Questions
Addforce to create a gliding effect? 2 Answers
How to add diving/rolling to FPSWalker script? (Not animation, just speed) 2 Answers
Can a CharacterController ignore one or more Colliders? 2 Answers
Stop player momentum carrying over when using a 'portal'? 1 Answer
Multiple players on single controller 2 Answers