Доброго дня всем. Делаю движение в 2d влево вправо и прыжок по пробелу. Все работает, но если прыгнуть близко к стене и в момент касания объектом стены или препятствия продолжать держать стрелки (влево - вправо), то персонаж не падает вниз, а зависает до отпускания кнопки.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Hero : MonoBehaviour
{
public GameObject Box;
bool is_ground = false;
private float speed = 5.0f;
private float jSpeed = 6.0f;
Rigidbody2D rb;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
Instantiate(Box, new Vector3(2, 5, 0), Quaternion.identity);
}
void OnTriggerStay2D(Collider2D col)
{ //если в тригере что то есть и у обьекта тег "ground"
if ((col.tag == "Ground") || (col.tag == "Box")) is_ground = true;
}
void OnTriggerExit2D(Collider2D col)
{ //если из триггера что то вышло и у обьекта тег "ground"
if ((col.tag == "Ground") || (col.tag == "Box")) is_ground = false;
}
// Update is called once per frame
// Update is called once per frame
void Update()
{
Debug.Log(is_ground);
if ((Input.GetKeyDown(KeyCode.Space))&& (is_ground))
{
jump();
}
}
private void FixedUpdate()
{
rb.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, rb.velocity.y);
}
private void jump()
{
rb.AddForce(transform.up * jSpeed, ForceMode2D.Impulse);
}
}