Object move and bounce off walls infinitely


I have a simple object in a square 3D room with walls.

I want to have an object move infinitely inside of it, and when it hits a wall I would like the object to bounce off like in the image.

This is my current script that doesn’t work correctly…

    [SerializeField] GameObject DirectionObject;

    private void Update()
        transform.position += DirectionObject.transform.forward * Time.deltaTime * 2f;

    private void OnCollisionEnter(Collision collision)
        if (collision.gameObject.CompareTag("Floor"))

        DirectionObject.transform.rotation = 
            DirectionObject.transform.localRotation.eulerAngles.y + 90,

I see you’re using transform to move the ball, i assume you have rigidbody on the ball since you’re using “OnCollisionEnter” event function.
Why do you move the player using the transform? is there any particular reason?

I suggest you use force, rather than changing the transform.
For infinite bounce you need to add both the ball and the walls a physics material with bounce value of 1.

I don’t know if you’ll get the exact results you’re looking for as in the image, but it all depends on the physics and the applied force on the ball.

Hope this helps.