Pushing rigidbodies and collisions

Hey guys,

I’m an experienced programmer in Javascript, HTML, PHP and a few other languages. I’m relatively new to Unity. I’ve spent the last two weeks going through docs and trying to figure out everything I can, as my company wants to develop a game of some kind (we haven’t decided yet). At any rate, I’m working on a series of demos to practice my technique and show what kinds of things we can do.

I’m learning okay, but I’m having a hard time wrapping my head around some of the physics things. Specifically, the two things that bother me and I can’t figure out are:

a) When a character controller pushes a rigidbody, the character will push into it, and the object will rotate, and the character will eventually climb on top of it once the slope of the object is enough for it to climb on top of it. I don’t want this. I want the rigidbody (a cube, in this case) to stay level on the ground and just move in the direction it’s being pushed. I’ve tried a few variations on the FPS push code, but nothing seems to really work. Any suggestions?

My first thought was to simply limit the rotation so that it can’t rotate at all, but I haven’t quite figured that out yet.

Here’s the push code, though, if it helps:

var pushPower : float = 2.0;
var pushDir : Vector3;

function OnControllerColliderHit (hit : ControllerColliderHit)
{
   var body : Rigidbody = hit.collider.attachedRigidbody;
   // no rigidbody
   if (body == null || body.isKinematic)
      return;
       
   // We dont want to push objects below us
   if (hit.moveDirection.y < -0.3)
      return;
   
   // Calculate push direction from move direction,
   // we only push objects to the sides never up and down
   pushDir = Vector3 (hit.moveDirection.x, 0, hit.moveDirection.z);

   // If you know how fast your character is trying to move,
   // then you can also multiply the push velocity by that.
   
   // Apply the push
   body.velocity = pushDir * pushPower;
}

b) Occasionally, for no apparent reason, my character will fall through the floor. I’m assuming this has something to do with the mesh collider, and perhaps the solution is to switch to a box collider. Am I on the right track?

I appreciate any and all help you guys can give me. I’ve been lurking in the forums for two weeks and reading every post I can get my hands on. Some of it’s over my head right now, but much of it has improved my knowledge of Unity immensely. Thanks for all you guys hard work!

For the first one, on the rigidbody, click the button that says ‘Freeze rotation’. That will keep it from rotating. If you want it to rotate when the CC is not hitting it, you could put something like:

hit.rigidbody.freezeRotation = true;
else {
rigidbody.freezeRotation = false;
}

You know, something like that…
As for the CC falling through, I dont know why this happens. but the physics engine seams to be not very good at collision detection. If you want, there is a box on the object the collider is attatched to, called Convex. Click it, and it might help…

Thanks for the tip on the first. It fixed the rotation thing (kind of obvious, but I sometimes forget to look at Inspector settings). The second part of the problem still exists – my CharacterController climbs on top of the object once it hits a wall.

I suppose I could check to see if I’m pushing a rigidbody, and if so, keep the Y-axis the same as the platform I’m walking on? Hmm.

I don’t know what the problem could be… wait, are you using the stock FirstPersonController prefab? if not, go to the CC component in the inspector, and change the skin width or whatever its called. Having it too thin can cause the character to go through and get stuck on objects…

Oh, hey. That worked. Set the skin to .01 (smaller than default) and it worked. No idea why. Time for some documentation research.

Thanks :slight_smile:

No problem. I love to help noobs :smile: :smile: