Overriding OnCollisionEnter function
I want to implement a function that does a lossless reflection of an object to be used in a billiard game. I am trying to override the OnCollisionEnter function in the collision of objects.
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.GetComponent<Rigidbody>())
return;
Vector3 inVec = rb.velocity;
Vector3 normal = collision.contacts[0].normal;
if (normal == Vector3.up) return;
if (Vector3.Dot(inVec.normalized, normal) < 0)
return;
Vector3 outVec = inVec - 2 * Vector3.Dot(inVec, normal) * normal;
rb.velocity = outVec;
}
This is the code I am using, but when I set the rigidbody (rb) velocity to outVec, it doesn't change the actual value (next frame it is unchanged)
How can I change the velocity so it actually affects the rigidbody?
Answer by Matthewj866 · May 19, 2017 at 06:47 PM
I am going to assume there is a reason for not letting the physics engine calculate the velocity if it's just for bouncing off other colliders.
The problem is not in the velocity assignment - the issue is the values you are putting in. This will only work if this collides with something else, rather than anything colliding into this object when it is stationary.
You need to adjust the maths and input here to take both objects' velocity into account if you're trying to manually assign a velocity based on a collision.
The reason is that when the balls hit the side, they roll along the side and not bounce off. I cannot get them to bounce consistently with a physics material. So I have a physics material on the balls that is used for bouncing off balls and my script is for bouncing off the walls. The issue with this is that in some circumstances, the ball bounces off the wall and then renters the collision and bounces back again doing a kind of ping pong/vibrate effect. The script was to fix this but it doesnt work.
Your original question was missing this crucial information - the issue you want solved only now makes it apparent in this comment.
I don't think we have enough information to fix colliders re-colliding with the ping-pong effect you've described to be honest, so as an educated guess if it's getting "stuck" in the other collider causing this effect you might want to cap the maximum velocity of objects so that they can't slip past the physics updates.
If this is not the issue, can you please provide us with more information?
Well I thought that when I override the OnCollisionEnter function it would only do my function, but it appears that it doesnt. I dont know if it is skipping past the update because it triggers the enter, exit then enter again so its like a double hit.
Answer by BroncoBilli · Oct 18, 2018 at 05:08 AM
I'm not sure anybody BOTHERED to answer his question. I have the same question. Why is it when I assign a velocity to the rigidbody in OnCollisionEnter, it doesn't affect my object. It's as if the physics engine overwrites it in the very next step. There should be a "collision.handled" event where if you set it to true, the physics engine leaves the calculation alone in the next step in the physics chain.
I will reask the question directly: Why, in OnCollisionEnter, if I set my object's rigidbody's velocity to some ludicrous #, does that value not take effect in the game, and my object just sort of moves a tiny bit. Unity is overwriting this value. why?