- Home /
My player wont jump (2d game)
I tried everything, but the player wont jump. Can you help?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class PlayerControler : MonoBehaviour
{
private Rigidbody2D PlayerRigidbody2D;
public float speed = 10f;
public float JumpForce;
private float MoveInput;
private bool FacingRight = true;
private SpriteRenderer mySpriteRenderer;
private bool IsGrounded;
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;
private int extraJump;
public int MaxJumps;
void Start()
{
extraJump = MaxJumps;
mySpriteRenderer = GetComponent<SpriteRenderer>();
PlayerRigidbody2D = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
MoveInput = Input.GetAxisRaw("Horizontal");
PlayerRigidbody2D.velocity = new Vector2(MoveInput * speed, transform.position.y);
if(MoveInput > 0 && FacingRight == false)
{
mySpriteRenderer.flipX = false;
FacingRight = true;
} else if(MoveInput < 0 && FacingRight == true)
{
mySpriteRenderer.flipX = true;
FacingRight = false;
}
}
void Update()
{
IsGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);
if (IsGrounded == true)
{
extraJump = MaxJumps;
}
if(Input.GetKeyDown(KeyCode.UpArrow) && extraJump > 0)
{
PlayerRigidbody2D.velocity = Vector2.up * JumpForce;
extraJump--;
} else if(Input.GetKeyDown(KeyCode.UpArrow) && extraJump == 0 && IsGrounded == true)
{
PlayerRigidbody2D.velocity = Vector2.up * JumpForce;
}
}
}
Comment
Answer by SSEthanCray · Mar 08, 2019 at 07:51 PM
You should start by eliminating certain parts of your code. When did the problem start?
Your answer
Follow this Question
Related Questions
Detecting Collision from top side of 2d box collider 0 Answers
I Can't Jump, And If I Remove if(isGrounded) I Can Do More Then 10 Jumps IN The Air 1 Answer
Problem with Jump 1 Answer
Unity2D UI detecting spawned object 0 Answers
2D Jump AI Help 0 Answers