- Home /
How to make ball bounce off screen boundaries?
Hello I have a script that keeps all my objects within the sides of the screen perfectly
public SpriteRenderer sprRnd:
void Update ()
{
Vector3 leftBottom = Camera.main.ViewportToWorldPoint(Vector3.zero);
Vector3 rightTop = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, 0));
float leftLimit = leftBottom.x;
float rightLimit = rightTop.x;
Vector3 extents = sprRnd.bounds.extents;
Vector3 pos = transform.position;
pos.x = Mathf.Clamp(pos.x, leftLimit + extents.x, rightLimit - extents.x);
transform.position = pos;
And I’m trying to get the ball to bounce off the screen boundaries I added box Collider2D rigidbody2D & a bounce material to the ball & ball force but when it hits the edge it does bounce off. I did create quads with box Colliders & it worked but I would prefer to use the screen as boundaries is this possible?
Answer by bennett_apps · Aug 15, 2018 at 03:19 AM
You could still use box colliders, just scale and move them to the size of the screen!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
2D ball same bounce height on objects as on the ground 2 Answers
Distribute terrain in zones 3 Answers
Balls bounce off wall in different ways 1 Answer
Bounce when hitting wall 2 Answers