Unity 2D Літати відбиваючись від стін

 

Rigidbody2D rb;
Vector3 lastVelosity;

void Start()
{
  rb = GetComponent<Rigidbody2D>();
  rb.AddForce(new Vector2(200f, 200f));
}

void Update()
{
  lastVelosity = rb.velocity;
}

private void OnCollisionEnter2D(Collision2D coll)
{
  var speed = lastVelosity.magnitude;
  var direction = Vector3.Reflect(lastVelosity.normalized, coll.contacts[0].normal);
  rb.velocity = direction * Mathf.Max(speed, 1f);   // 1 - min speed
}