ResetInputAxes/Disabling input not working until user is finished moving?

Hi,

I’m currently working on an on-rails flight game, and I’ve set about a trigger zone for the boundaries on the stage. At the left-most boundary, when a trigger collision is detected, a message is sent to the ship’s control script; this informs the ship to do the following;

  1. Trigger the “crashed” method for 0.2 seconds; users cannot enter any input when their ship is “crashed”, effectively disabling the controls.

  2. A force is applied to the ship, to nudge it away from the boundary

  3. Input.ResetInputAxes(); is called, to prevent against the player from repeatedly “bumping” up against the boundary.

  4. Moving left is disabled for the duration that the player is touching off the boundary - allowing them to only move right (out of the boundary zone), and up/down.

However, none of these appear to work - until the player stops moving. If the player holds down the button to move left, they will run straight through the boundary - with no attempt made by Unity to proceed with the ResetInputAxes call, or disable the controls temporarily.

If the player stops moving (rather, stops holding down the button to move left), then Unity will recognise the script, and will prevent any additional movement towards the boundary.

It’s as if Unity cannot run the ResetInputAxes method, nor check the conditionals for movement, while the player is moving. Throwing in a quick print/debug.log call shows that the method itself runs fine; but it’s like ResetInputAxes is queued on a stack until the player stops moving themselves.

Would anyone have any ideas on what could possibly cause this?

I started playing around with it and I think it’s easier to do this with normal physics instead of triggers.

  • Your border should have a non-trigger box collider
  • Your player should have a rigidbody + non-trigger collider
  • Instead of OnTriggerEnter, you use OnCollisionEnter, note that it takes a Collision class as parameter.

I don’t know if you were using transform.Translate or transform.ApplyForce before, but you need to use transform.ApplyForce. Only ApplyForce will change the velocity of the rigidbody.

If you absolutely must do it with triggers, you will have to use transform.ApplyForce and have it apply the opposite velocity to the collider to add the nudge you were talking about. That is, left&right borders apply col.rigidbody.velocity = new Vector3(-x,y,z); and up & down apply col.rigidbody.velocity = new Vector3(x,-y,z); on OnTriggerStay. Then OnTriggerExit sets velocity to 0.