Question by
wagnerraurau · May 15 at 02:10 AM ·
c#unity 2d
why the jump doesn't works?
I'm trying to make a simple jump, but it's not working at all, for some reason the IsGrounded()
is not working, it keeps with the infinite jump.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Move_incremented : MonoBehaviour
{
[SerializeField] private LayerMask LayerMask;
Rigidbody2D rb;
BoxCollider2D box2d;
float speed = 10.0f;
float jump = 7f;
float dir_horz;
void Start()
{
rb = GetComponent<Rigidbody2D>();
box2d = GetComponent<BoxCollider2D>();
}
void FixedUpdate()
{
Move();
Jump();
}
void Move()
{
//movimentação
dir_horz = Input.GetAxis("Horizontal");
rb.velocity = new Vector2 (speed * dir_horz, rb.velocity.y);
//flip
if(dir_horz > 0.01f)
{
transform.localScale = new Vector3(5, 5, 1);
}
else if(dir_horz < -0.01f)
{
transform.localScale = new Vector3(-5, 5, 1);
}
}
void Jump()
{
//pulo
if (IsGrounded() && Input.GetKey(KeyCode.Space))
{
rb.velocity = new Vector2 (rb.velocity.x, jump);
}
//pulo duplo
}
bool IsGrounded()
{
RaycastHit2D raycastHit2d = Physics2D.BoxCast(box2d.bounds.center, box2d.bounds.size, 0f, Vector2.down * .1f, LayerMask);
return raycastHit2d.collider != null;
}
}
Please, help me.
Comment
Answer by TheIrishKraken · May 15 at 10:14 PM
Hello, inside your IsGrounded method you are multiplying Vector2.down by .1f. This is what's causing your infinite jump. There needs to be a comma between Vector2.down & .1f.