Question by
iSizz · Jul 15, 2019 at 12:00 PM ·
2dscripting problemjumpplatformerground detection
Ground check doesnt work
Hello, i have a little problem here, so i made a some kind of a ground check to stop jumping in the air, but it doesnt work, i still can jump in air.. Please help.
{ public float speedX; public float jumpSpeedY;
bool facingRight, CanJump;
float speed;
Animator anim;
Rigidbody2D rb;
void Start()
{
anim = GetComponent<Animator>();
rb = GetComponent<Rigidbody2D>();
facingRight = true;
}
void Update()
{
MovePlayer(speed);
if(Input.GetKeyDown(KeyCode.LeftArrow))
{
speed = -speedX;
}
if(Input.GetKeyUp(KeyCode.LeftArrow))
{
speed = 0;
}
if(Input.GetKeyDown(KeyCode.RightArrow))
{
speed = speedX;
}
if(Input.GetKeyUp(KeyCode.RightArrow))
{
speed = 0;
}
if(Input.GetKeyDown(KeyCode.UpArrow))
{
if(CanJump = true)
{
rb.AddForce(new Vector2(rb.velocity.x, jumpSpeedY));
}
}
}
void MovePlayer(float playerSpeed)
{
if(playerSpeed < 0|| playerSpeed > 0)
rb.velocity = new Vector3(speed, rb.velocity.y,0);
}
private void Flip()
{
facingRight = !facingRight;
Vector3 temp = transform.localScale;
temp.x *= -1;
transform.localScale = temp;
}
void OnCollisionEnter2D(Collision2D other)
{
if(other.gameObject.tag == "ground")
{
CanJump = true;
print ("can jump");
}
}
void OnCollisionExit2D(Collision2D other)
{
if(other.gameObject.tag == "ground")
{
CanJump = false;
print ("cant jump");
}
}
public void WalkLeft()
{
speed = -speedX;
}
public void WalkRight()
{
speed = speedX;
}
public void StopMoving()
{
speed = 0;
}
public void Jump()
{
if(CanJump = true)
{
rb.AddForce(new Vector2(rb.velocity.x, jumpSpeedY));
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Checking if the player jumps while not grounded 0 Answers
how long a bottom being pressed [solved] 3 Answers
Unity2D Player Jump Heights Inconsistent,Unity2D Inconsistent Player Jump Heights 0 Answers
Enemy jump in unity 2d 0 Answers
Jumping on enemies help 0 Answers