прыжок в юнити 2д
есть вот такой у меня код для движения игрока.
беда в том что прыжок не срабатывает с первого раза. иногда приходится по 5 раз нажать чтобы прыгнуть, хотя все булевые переменные активны.
3 переменных потому что игрок широкий и надо захватывать от края до края.
если у вас есть метод по лучше, расскажите. или помогите решить мою проблему ._.
public class player_controler : MonoBehaviour
{
Rigidbody2D rb;
public bool[] isGrounded = new bool[4];
public float speed = 6;
public float JumpForse = 15;
public float dirX;
public LayerMask what_is_earth;
public float ground_pos_x = .29f;
public float ground_pos_y = -.37f;
public float radius = .2f;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
public void FixedUpdate()
{
// jump
{
isGrounded[1] = Physics2D.OverlapCircle((Vector2)transform.position + new Vector2(0, ground_pos_y), radius, what_is_earth);
isGrounded[2] = Physics2D.OverlapCircle((Vector2)transform.position + new Vector2(-ground_pos_x, ground_pos_y), radius, what_is_earth);
isGrounded[3] = Physics2D.OverlapCircle((Vector2)transform.position + new Vector2(ground_pos_x, ground_pos_y), radius, what_is_earth);
if (isGrounded[1] || isGrounded[2] || isGrounded[3]) isGrounded[0] = true;
else isGrounded[0] = false;
if (Input.GetKeyDown(KeyCode.Space) && isGrounded[0])
rb.velocity = new Vector2(rb.velocity.x, JumpForse);
}
// run
{
dirX = Input.GetAxis("Horizontal") * 1.5f;
rb.velocity = new Vector2(dirX * speed, rb.velocity.y);
}
// flip
{
if (dirX >= 0.5) gameObject.GetComponent<SpriteRenderer>().flipX = false;
else if (dirX <= -0.5) gameObject.GetComponent<SpriteRenderer>().flipX = true;
}
}
private void OnDrawGizmos()
{
Gizmos.DrawWireSphere((Vector2)transform.position + new Vector2(0, ground_pos_y), radius);
Gizmos.DrawWireSphere((Vector2)transform.position + new Vector2(-ground_pos_x, ground_pos_y), radius);
Gizmos.DrawWireSphere((Vector2)transform.position + new Vector2(ground_pos_x, ground_pos_y), radius);
}
}