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 ![]()
Thank you for helping!