I have active/jet bumpers in a pinball sim. I want the game’s ball to have more velocity after striking them than it did on its approach (to simulate the bumper striking the ball back upon impact). I’ve been toying with code in the evenings for a week, so I think sufficient time has passed to admit I don’t know what I’m doing after reading APIs and examples and would still like to ask for help.
Current code, attached to a bumper:
function OnCollisionStay(collision : Collision) {
var contact = collision.contacts[0];
// var normal = transform.TransformDirection(Vector3.forward);
direction = contact.otherCollider.transform.position - transform.position;
contact.otherCollider.attachedRigidbody.AddForceAtPosition(direction.normalized * 100, direction);
}
function OnCollisionExit(collision : Collision) {
if(collision.rigidbody){
collision.rigidbody.velocity *= 1.5;
}
}
I think that’ll work.
function OnCollisionEnter(collision : Collision) {
if(collision.rigidbody){
collision.rigidbody.velocity *= -1.5;
}
}
Wouldn’t you want it like that, to reverse the direction?
Krispy and Moldorma, thank you very much for your kind assistance. Interesting problems sometime occur with either code. I shall need to examine this carefully.
Hi,
I tried out these suggestions and went on to work on other parts of my project for awhile. The way the two functions in the subject line are implemented in my game, neither is completely suitable, unfortunately.
If OnCollisionEnter is used as in the example above and, say, the ball rolls down the playing field and strikes the active bumper, the ball will bounce back up in the exact opposite direction, which is not desirable. Even if it looks like the ball shouldn’t be going straight back up due to the angle of the colliding surfaces, it will act like the ball struck something flat.
If I let the physics engine handle the collision instead and wait until the ball has left the collision area before doing anything to the ball’s velocity (OnCollisionExit) as in the other example above, then I run the risk of the ball hitting the bumper softly and rolling against it until the ball slides off again, abruptly changing its velocity when it’s finally no longer in contact, which will be an inappropriate location and inappaopriate resultant velocity.
So, I was wondering what a better way of doing this would be.
Thanks very much.
You need an invisible “PUSH” not to alter the velocity of the object but to add a push to the object by another object which in turn changes the velocity of the ball. Just like a regular spring bumper works. Both examples they gave are changing the velocity of the ball directly, you need an indirect change.
The bumper is a circle with a radius, on collision enter enlarge the bumper rapidly which will push the ball and then shrink the bumper back down after it reaches max diameter change. Even the slightest diameter change in rapid concession will effect the velocity of the ball hitting it and give you the desired results.