Restricting Rigidbody Upward Movement in the Y Axis

Hi there, I wondered if anyone could advise me the ‘best’ way to restrict the movement of a rigidbody in the Y (up) axis. I don’t mean using the rigidbody constraints in which you can specify no movement in the Y, as I do want objects to fall, I just don’t want them to move vertically if a force is applied to them. I do incidentally want them to move in the X axis.

I guess I could achieve this by vastly increasing it’s mass/gravity, but is this really the best method? Many thanks for all suggestions

1 Answer

1

Hand force it in code (also fixes many of those “rats – I spawned touching something and now it wants to blast into space”):

if(rigidbody.velocity.y>0) {
  Vector3 v = rigidbody.velocity;
  v.y=0;
  // or: v.y *= 0.5f; // extreme y up drag
  rigidbody.velocity = v;
}

If you can jump, you can manually keep track of yUpMax, adjust for gravity each frame, and check against that instead of 0.

Can also save oldVelY each frame and have OnCollisionEnter reset it (haven’t tested this much, but I believe Collision events trigger after physics has done its thing to the speeds.)