- Home /
Unrealistic bounce
Hi, i'm coding a minigolf like game, but i'm having difficulties with the bounce. Currently, i'm using a bounce material, but the problem is, when the ball has a really low angle, it will slide on the wall. I tried to then code myself a script, using a wall with a trigger, but I don't know how to get the "normal vector" of the wall. You see, wall can be vertical, horizontal, curvy, etc., and I can't (at least I don't want) create a script for each sitation. So is there a way to get the "normal vector" in a "onTriggerEnter" or a "onCollisionEnter", or can I create my own physic material to do so? Is there any other way?
Answer by whydoidoit · Jun 09, 2012 at 11:28 AM
Well you can get the contacts in OnCollisionEnter from the collision parameter and that gives you the normal of each contact point.
So, I tried to make my own bouncing script, using the contacts in OnCollisionEnter, but results are quite specials.
Here is my code:
void OnCollisionEnter(Collision other) { Debug.Log("bounce");
if (other.gameObject.tag == "bouncy")
{
velocity= Vector3.Reflect (gameObject.rigidbody.velocity,other.contacts[0].normal);
}
gameObject.rigidbody.velocity = velocity;
} Depending on the angle of the ball, it will work sometimes, and sometimes won't. And the speed of the ball changes as well, whereas i'm trying to keep the same speed (i'm in a space-like environnement, without frictions). So, some time the ball bounces, and some time it stucks on the wall. Any idea?
Hmmm:
are you actually getting more than one contact?
can you try going back to the ordinary physics and reducing the $$anonymous$$imum bounce speed in the physics settings?
Answer by iraldir · Jun 09, 2012 at 11:30 AM
that sounds promising, i'll try this, and give a feedback to you. Thanks
Just one thing - on Unity Answers "Answer" means "Solution" and not "Reply" - there's an Add New Comment link hidden over on the right of the screen - use that rather than Answer as they get cleaned up when they are not a solution to the question :) Everybody does it first time :)