Hello.
I have a ball flying towards the glass pane (the glass pane is replaced with broken glass prefab with shards that are rigidbodies) and the problem is that ball loses all it impulse after it hits the pane.
How can I make the ball to lose its impulse and velocity partially?
The easiest way to solve this problem would be to set the IsTrigger property of the collider on the unbroken glass. When the trigger is hit, you could then slow ball:
rigidbody.velocity = rigidbody.velocity * 0.5;
Or it is possible you can just let the shards slow the ball. Be sure that the shards have a low mass, and place them in front of the ball. The easiest way would be for the trigger collider to be slightly in front of the unbroken glass.
I have breaking objects in a game I’m working on. What I do is have 2 prefabs for each breakable object, just as you do. To solve the problem you described, you’ll need to reassign the balls velocity.
Alright, so the biggest problem is actually getting the ball’s velocity from inside OnCollisionEnter(), so it’s usually a lot easier to just save the last velocity, that’s happening in FixedUpdate().
And since OnCollisionEnter() is called after the actual collision was processed, you can assign a velocity to the ball without trouble after swapping the glass for the broken glass prefab.
var lastVelocity : Vector3;
var slowAmount : float = 0.4;
function FixedUpdate () {
lastVelocity = rigidbody.velocity;
}
function OnCollisionEnter (col : Collision) {
//swap the prefabs
rigidbody.velocity = lastVelocity * slowAmount;
}
–David–