Персонаж проходит сквозь стены как можно исправить? (Вид игры сверху, персонаж двигается во все стороны)
Код
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class BOX : Unit
{
[SerializeField]
private int lives = 3;
[SerializeField]
public float speed = 3.0F;
private bool isGrounded = false;
private Vector2 direction;
new private Rigidbody2D rigidbody;
private SpriteRenderer sprite;
private void Awake()
{
rigidbody = GetComponent<Rigidbody2D>();
sprite = GetComponentInChildren<SpriteRenderer>();
}
private void Start()
{
direction = Vector2.up;
}
private void FixedUpdate()
{
CheckGround();
}
private void Update()
{
GetInput();
Run();
}
private void Run()
{
transform.Translate(direction*speed*Time.deltaTime);
}
private void GetInput()
{
direction = Vector2.zero;
if (Input.GetKey(KeyCode.W))
{
direction += Vector2.up;
}
if (Input.GetKey(KeyCode.A))
{
direction += Vector2.left;
}
if (Input.GetKey(KeyCode.S))
{
direction += Vector2.down;
}
if (Input.GetKey(KeyCode.D))
{
direction += Vector2.right;
}
}
public override void ReceiveDamage()
{
lives--;
rigidbody.velocity = Vector3.zero;
rigidbody.AddForce(transform.up * 8.0F, ForceMode2D.Impulse);
if (lives < 1) SceneManager.LoadScene("Death");
Debug.Log(lives);
}
private void CheckGround()
{
Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, 0.3F);
}
private void OnCollisionEnter2D(Collision2D collider)
{
if (collider.gameObject.name == "Portal")
SceneManager.LoadScene("Ekran");
}
private void OnTriggerEnter2D(Collider2D collider)
{
Unit unit = collider.gameObject.GetComponent<Unit>();
if (unit)
{
Debug.Log("OK");
ReceiveDamage();
}
}
}