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!