- Home /
Preventing objects from intersecting with minimal physics
I've been working on this problem for a while with little luck. I have a game in which the player controls a spinning box. Its movement controls are entirely handled by C# script to keep them unaffected by physics, and there is supposed to be no gravity in the environment since it's set in a flying dream. I don't want to add anything to my objects that will cause them to be affected by any physics except collisions. My test obstacle is a huge rock (a sphere), which I want to remain stationary no matter how much the player pushes against it.
From the combinations I've tried, usually the box goes through the rock. Adding a character controller to the player didn't work. Adding an identically-sized non-kinematic rigidbody as a child to the player (to avoid physics problems) did nothing. Having the rigidbody on the rock allows me to push it around, which I don't want, and setting constraints on any axis allows the box to intersect it.
My closest success is having a collider on the rock, and a collider and rigidbody on the player box, but while that makes the rock stationary like I want, when I push the box against the rock, the box jitters, and generally floats around strangely. Checking kinematic causes it to pass through the rock. Doing that, then adding a non-kinematic rigidbody to the rock stops the intersection, but allows it to be pushed around by the box.
Does anyone have any ideas about this?
this is very confusing. could you post a screen shot of your setup ?
Answer by CloverKitsune · Apr 19, 2013 at 06:46 PM
I tried something new, which seems to solve the problem, although I'm not experienced enough to know if it's the best way.
I put a kinematic rigidbody (and collider) on the rock, and non-kinematic rigidbody (and collider) on the box, but changed drag and angular drag to infinity. That seems to work.
Would this cause any unforeseen problems, or is this a good way to handle it?
Answer by cjdev · Apr 19, 2013 at 09:58 AM
If the rock is an exact sphere, can't you hard-code the corners of the box to not pass the radius of the rock?
I'm not sure how I'd do that, and since it's just a test object, I want to make sure it'll work for obstacles of other shapes.
Normally, it isn't a problem because an obstacle collision will destroy the player, but when the player is invincible, the box just pushes obstacles around, which I don't want since the level is like an obstacle course.
The only thing I can think of is adding the non-kinematic rigidbody to the rock and freezing position and rotation in the constraints to prevent it from moving.
Unfortunately, when I do that and freeze all constraints, it allows the box to pass right through the rock. If I constrain just position, the box still intersects. If I constrain just rotation, the box pushes the rock around.
Answer by Reyan · Apr 19, 2013 at 07:48 PM
Rigidbody also has a checkbox "Use Gravity." Did you try unchecking that?
If that doesn't work, I'd suggest altering your script to move the rocket perhaps via ConstantForce, which should allow collision to work normally.
I unchecked gravity. The problem seems to have been drag.
Your answer
Follow this Question
Related Questions
How to setup character Collisions? 2 Answers
Realistic collisions with a kinematic rigidbody? 1 Answer
How to properly move a rigidbody/collider? 2 Answers
Layers are colliding even though they are set to ignore each other 1 Answer
Having a kinematic rigidbody detect collision with a collider without a rigidbody 7 Answers