another bouncing ball...

Hi there!
I have a question on how to make a ball bounce on a wall and bounce back with the precise angle wrt the normal of the wall itself.

Since I don’t need physics to make the ball bounce I made a little script(just a test script with the ball put in a boz made of 4 walls and bouncing over…):

var force = 15;
private var direction;

function Awake() {
	direction = Vector3(3,0,3);
}

function FixedUpdate () 
{	
	var move = direction * force * Time.deltaTime;
	var newPos = rigidbody.position + move;
	rigidbody.MovePosition(newPos);	
	Debug.Log("Direction:" + direction);
}

function OnCollisionEnter(collision : Collision) 
{
	Debug.Log("collision! Collider: " + collision.collider.name);
        //retrieving where the contact happened
	var contact = collision.contacts[0];
	newDirection = Vector3.Reflect(contact.point, contact.normal);
	newDirection.Normalize();
	newDirection.y = 0;
	direction = newDirection;
}

@script RequireComponent(Rigidbody)

I use Vector3.reflect to get a specular direction, but it seems to work bad. I don’t know if I’m considering a wrong normal or if everything has to be re-built. How can I check the normal of an object in unity? As a debugging feature I mean…

I attached the scene I’m working with just to give you the idea of what i’m doing :slight_smile:

Thank you for helping!

390620–13430–$bouncer_295.unitypackage (286 KB)

I noticed only now that I left the “is kinematic” flag on in the attahced scene and something else I changed for debugging…

Attached there is the real scene… sorry!

well… I tested the normals on the walls and they look right. What can it be then?
The reflect command isn’t suited for the purpose i used it?

390679–13431–$bouncer_130.unitypackage (286 KB)

ok… solved it… just a stupid trivial error :stuck_out_tongue:

instead of the direction of incoming collision i gave to Vector3.Reflect the point of collision.

The code changes as follows:

...
	newDirection = Vector3.Reflect(direction, contact.normal);
...

-_-’

The next step is introducing some kind of random variability in the reflection angle (adding or subtracting some degrees), just to make the thing less predictable.