Движение объекта до стены, Unity2D
Я хочу двигать кубик, пока он не коснется стены при нажатии на шифт. Впринципе это работает, но если нажимать шифт прямо у стены, то кубик все равно проходит сквозь ее. Как это можно поправить?
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
axisX = Input.GetAxis("Horizontal");
axisY = Input.GetAxis("Vertical");
if (Input.GetKey(KeyCode.LeftShift))
Move();
if (Input.GetButton("Horizontal"))
{
rb.velocity = new Vector2(axisX * speed, 0);
}
if (Input.GetButton("Jump"))
{
rb.AddForce(Vector2.up * 3);
}
}
void Move()
{
if (!gameObject.GetComponent<Collider2D>().bounds.Intersects(wall[0].GetComponent<Collider2D>().bounds))
{
gameObject.transform.position += new Vector3(axisX, axisY, 0);
}
else {
gameObject.transform.position += new Vector3(0, 0, 0);
}
}
}