Colliders not really working

So basically I’m making a Star Fox rip off. I have box colliders on the left, right, and top, and the ground object has a mesh collider. My ship has a box collider and a rigidbody.
The controls basically take the input from the left analog stick, apply some math so it’s the right speed that feels good, and then make that the velocity of the up/down and left/right of the rigidbody of the ship (the forwards stays constant at 30, so it’s always moving forwards, which I might change up to be variable to I can speed up or slow down depending on if the appropriate button is pushed).

So essentially, my ship object is moving around by taking the analog stick values, and making that to the rigidbody velocity. What ends up happening is the ship will go right through the colliders most the time, but not all the time. I’ve tried the same thing by making the stick values change the force, rather than the velocity, which I liked because it felt more floaty, but there was the same problem (it ignores colliders quite often).

If I just make gravity apply to my ship and don’t push anything, it falls perfectly, and lands on the mesh collider, and rides it, going up and down with the terrain, which is awesome. If I have no gravity (so it’s a ship that actually flies), and I fly into the terrain straight (no buttons pushed), it will ride the terrain. If I push buttons to make it go into the ground, a lot of the time it ignores the collider, and goes right through the floor. Same thing with left and right.

I’m definitely very new to Unity, but basically any time I’ve tried using colliders, it seems they get ignored a lot. What it feels like is that the collider is at a certain value (like a plane on the x axis at value 30, for example), and if the object goes from >30 and ends up being <30, it will just ignore the collider. It feels like it has to be exactly on top of the collider to actually recognize it, or else it just bypasses it entirely. If that’s the case, how do I make it work? Am I wrong? If so, what’s happening, and how do I make colliders work correctly?

I absolutely need mesh colliders so that my ship actually interacts with the environment and can’t just fly through the objects and ground and what not (or else it’d be a pretty lame game).
Thanks for any help you can provide!

Don’t set the rigidbody velocity directly. Use rb.AddRelativeForce(transform.forward * wantedVelocity* rb.mass, ForceMode.Force). You may want to consider a PID controller for stabilizing the rigidbody as well as some counter-drifting code to stop side-sliding when turning.

2 Likes

Are you ever applying values to transform.position or rigidbody.position on the objects which are ignoring collisions? If so then it’ll be intermittently overwriting the physics system’s attempts to handle collisions.

For future reference, this should probably be in the Scripting section.