- Home /
Speed Ball script
I have a ball that roll on a plane. How I can make it speed up when it hit a collider?
Answer by duck · Nov 23, 2010 at 07:12 AM
Add this to the script to any objects which should give an extra kick to the ball when they collide with it. This is useful in situations such as a pinball table, where only certain objects should give the ball a strong kick. (This script assumes the ball is tagged "Ball").
var bounceForce : float = 2;
function OnCollisionEnter(c : Collision) {
if (c.collider.CompareTag("Ball")) {
// give the ball extra force in the direction that its currently moving:
var rollDirection = rigidbody.velocity.normalized;
rigidbody.AddForce( rollDirection * bounceForce );
}
}
Alternatively, you could probably implement this using custom Physic Materials, and set the bounce value to a high number, however I would go with the scripting method, because generally you would need a handler like this to perform other actions too such as playing a sound effect and adding score, so my feeling is that it would make sense to have these things grouped together as a script, but that's up to you!
Answer by ovechkin1308 · Nov 23, 2010 at 12:48 PM
But how I can say to it that I want one just one collider
Answer by ovechkin1308 · Nov 23, 2010 at 01:39 PM
It said to me that i'm not allowed to call get_rigidbody in a variable.
Your answer

Follow this Question
Related Questions
this script is to reset the ball to its initial position after every kick in foorball game.. 1 Answer
Need help with begginner script! 1 Answer
Ball wont go faster 2 Answers
Ball movement script 3 Answers