- Home /
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?
Can you post code snippets? I think the problem is in your input disabling. If your input isn't disabled properly, one call to ResetInputAxes will not be enough, as the player's input keeps on having an effect.
Sure - when you trigger the left boundary, the following is called;
void OnTriggerEnter(Collider col) {
if (col.gameObject.name == "PlayerContainer") {
Debug.Log("found player container"); col.gameObject.Send$$anonymous$$essage("leftBoundary");
}
}
Following this, leftboundary contains;
public void leftBoundary() {
float y = rigidbody.velocity.y;
float z = rigidbody.velocity.z;
rigidbody.velocity.Set (0f, y, z);
leftBoundaryHit = true;
Input.ResetInputAxes();
Debug.Log ("Reset axes");
crashed ();
}
crashed is what takes the player's control away for 0.2s;
public void crashed() {
//disable controls
canControl = false;
hasCrashed = true;
}
and finally, under Update, the first thing done is the crashRecovery method is called, is hasCrashed = true, which simply counts up to 0.2s and then re-enables controls;
public void crashRecovery() {
if (canControl == false && hasCrashed == true) {
if (crashTimer < crashRecoveryTime) {
crashTimer += Time.deltaTime;
} else {
crashTimer = 0;
canControl = true;
hasCrashed = false;
}
}
}
Lastly, under update, we have our control scheme - before any input can be recognised, the canControl bool is checked - and user input will only be recognised if this is set to true.
i.e;
if (canControl) {
if (Input.GetAxis("Horizontal") != 0) {
..etc etc..
}
if (Input.GetAxis("Vertical") != 0) {
..etc etc..
}
}
Unity just doesn't seem to ever want to check this canControl bool while the user is still holding down a button - it's almost as though every button runs in it's own thread, and the canControl conditional does not get rechecked until a new button is hit, or one is left go.
Answer by Jamora · Aug 01, 2013 at 02:30 AM
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.
Hey,
Thanks for your reply - genuinely, I really appreciate you taking the time out to help me on this!
I did consider using a standard box collider, but the idea was to have two seperate colliders - one being a trigger to slow the player down, and the other to be a dead stop.
I generally use rigidbody.addforce - I don't think there is an applyforce under either the rigidbody or transform classes? There definitely shouldn't be under transform at least..
I did attempt to apply the opposite force to the player entering the container, but found that it was far too weak to push the player away, for whatever reason. But still, there is still the matter that the player is still capable of controlling their ship, even when I disable controls - as long as they don't leave go of a button, or press any new buttons. Surely this should not be possible? If this really is the case, it's surely an engine bug?
You are correct, it is AddForce.
Have you checked that your object truly is uncontrollable by increasing the time from 0.2 seconds to say, 2seconds? Because in my test the object was uncontrollable. $$anonymous$$aybe you have two controller scripts on the player?
You can achieve the dead stop by making your border's collider normal without a rigidbody. As for the slowing trigger, are you using OnTriggerStay to apply the force? If you only apply it in OnTriggerEnter, it'll have to be a big pulse. OnTriggerExit the velocity will have to be set to 0.
Your answer
Follow this Question
Related Questions
Lightswitch help? 3 Answers
Multiple Cars not working 1 Answer
Activate an input to pickup or search objectes 1 Answer
Distribute terrain in zones 3 Answers
Help With Player Decrease Enemy Health!,Need Help with Character 1 Answer