I hope you guys can help me out because I am not really sure how to fix this.
I saw similar questions here but all for 2D space, not 3D as I have.
People recommended messing with the “bounce threshold setting”, but I still have issues.
I have a ball that is bouncing around programatically inside a 3D environment.
The ball (with a rigidbody) is inside a cube (with a mesh collider). I have a little thingy (Mesh) controlled by me that it can bounce on to make it go different angles.
I initiate the ball adding a force to it in an axis, and I have a script that, on collision, reflects its velocity.
The ball has a physic material with the following properties:
Dynamic friction = 0
Static friction = 0
Bounciness = 1
Friction combine = minimum
Bounce combine = maximum
It will just bounce around just fine, but many times, when the ball hits a corner, since they are 90 degrees corners, I feel like the normals cancel each other and the ball either loses bounce in a direction or gets completely stuck if it had hit a 3 wall corner.
My code on the ball:
public class BallMovement : MonoBehaviour {
public float thrust;
Rigidbody rb;
private Vector3 tempVelocity;
void Start() {
rb = this.GetComponent<Rigidbody>();
rb.AddForce(0, 0, thrust, ForceMode.VelocityChange);
}
void OnCollisionEnter(Collision other) {
tempVelocity = rb.velocity;
tempVelocity = Vector3.Reflect(other.relativeVelocity*-1.01f, other.contacts[0].normal); //here I invert the direction of the ball
rb.velocity = tempVelocity; // assign the ball's velocity the new value
}
}
Thing I tried: checking for the velocity going below a certain value and if it did jump it up to an arbitrary minimum value. It didn’t work, ball still gets stuck to edges.
Some other data that might point to misconfiguration: I did the Reflect Vector3 method because just using the material I made and uncheck gravity influence on the object wasn’t bouncing it in a perfectly elastic form, it would eventually stop as if there was “friction” in the air.
Something I read on another thread: would making the object Kinematic help? With the code I currently have, switching that option on does not move the ball at all at the beginning. Makes sense, because I am using force to move it around.
There’s something else that would be nice to have and could be related to fixing this. Sometimes the angle the ball bounces at is too sharp and makes for annoying movement. I want to remove realism from this, so I think what I need to do, is make sure the normal of the object it is colliding with is within a specified “range” so it doesn’t do movements I do not want. Unfortunately for me, I have forgotten all my angle/vector math from school and I have no idea how to go at this
Would that solution also fix the weird behavior of the ball getting stuck to the corners?
Any help appreciated.
[EDIT] Noticed what was wrong with normal physics not working, it was that Bounce Threshold factor. Reducing it made the ball bounce even if I disable my Reflect code. But the ball still does stuff like roll on a wall. So I think I have to limit the normals coordinates as I thought before, so I can always make it bounce in a desirable angle, even if it isn’t realistic!