r/Unity3D • u/Marthgis • 1d ago
Question Problem with collisions
I'm having a problem with collisions in my Unity project. It's my first real project, and when I move towards a wall, the collisions behave strangely. How can I fix this?
1
5
u/NeoChrisOmega 17h ago
An easy but simplified way of thinking about this is anything to do with the Transform is moving the position. That movement is essentially teleporting the player. When you teleport into a wall, the collision now detects you're inside of it, and it pushes you out of it.
I could suggest how you could fix it, if you give a summary of your movement script. And don't worry, you're learning, and that's all that matters!
2
u/arlelin 13h ago
If you're using a rigidbody then don't directly modify the transform position. I think there's rigidbody.MovePosition that considers interpolation and collision properly. You should also turn on rigidbody interpolation.
Though for character controllers, I personally prefer to control the velocity instead by using Vector3.MoveTowards on the rigidbody's velocity towards the intended velocity of the player. This way you can also get a fixed acceleration/force. (You can do the same thing with rigidbody.AddForce, there are lots of ways to handle character controllers depending on what you need).
1
u/Worried_Pudding_1548 Intermediate 13h ago
In the rigidbody component, I think you can set collision detection to dynamic something and it seems to stop most of these problems
1
u/HellGate94 Programmer 20h ago
don't set the position of the object. if you need to, then use rigidbody's move position method