#Vector3.Reflect bouncing ball issues

1 messages · Page 1 of 1 (latest)

barren dove
#

I am having some issues with making a ball bounce off walls in 3d unity.
Below is my code:

My fixedupdate:

rb.AddForce(movement * speed);

private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.layer == LayerMask.NameToLayer(layerMask))
{
// Get the first contact point
ContactPoint contact = collision.contacts[0];
Vector3 normal = contact.normal;
Vector3 reflected = Vector3.Reflect(rb.linearVelocity, normal);
rb.linearVelocity = reflected * 0.8f; // 0.8f = keep 80% of speed

}
}

(Please let me know if you need the full code to help out)

It works completely fine when colliding with an object with a rigidbody but when colliding with any object it just stops the ball.

I have ensured the ball I am applying forces to is set to interpolate and continuous.
The wall it is hitting has a box collider and the correct layer mask, on trigger is unchecked and nothing is kinematic.

I feel like im missing something simple here. New to unity so any help would be appreciated

thick pendant
#

Haha oh god, I had this exact issue not long ago

#

Like to the damn letter, this exact thing.

I'm just trying to find the repo where I fixed this issue but until then, what you can do is add a physics material to the wall (and maybe the ball too) with a high Bounciness value (like... close to 1, if not 1 exactly)

#

for the ball's physics material, set Bounciness to 1
for the wall's physics material, Bounciness can be lower. I had mine set to 0.6 but ymmv, adjust as needed

for both, set the Bounce Combine to Maximum

#

And I have just found the commit where I fixed the issue.

It was combining this method, with code almost identical to what you have written (though I had a field instead of a hardcoded 0.8f so it could be adjusted in the editor easily)

I found that for some reason, setting the Bounciness alone still had the ball stick. But code like you have also had it stick but in different situations. The code and the Bounciness value together eliminated the problem. I still do not know why but there we go

#

@barren dove hope that helps

barren dove
barren dove
#

Incase anyone comes across the same issue, I managed to figure this out without the need for a physics material. Just stored the velocity from late update then used that as it was the “last velocity” before unity physics sim stopped the ball