How can I ignore user input based on a condition?

I am trying to stop a turret from rotating once the rotational limit is reached. Not sure how to use the EventType.Ignore as I am a total noob (but a fast study) to scripting in general. Any help is much appreciated. Thanks.

    // Obtain the gun's forward component in the horizontal plane
    tHorizComp = gun.forward - (Vector3.Dot(gun.forward, Vector3(0,1,0)) * Vector3(0,1,0));

    // Now get the angle between the turret base's forward vector and the gun forward's horizontal component
    tAngle = Vector3.Angle(tHorizComp, base.forward);

    // See if that angle is too large
    if (tAngle > yMaxRotation) 
    //if too large then ignore input in that direction



Try this:

  • Get user input
  • Change the rotation based on user input
  • Clamp the rotation using Mathf.Clamp()
  • Change the gun to the new rotation after clamping

All clamping does is ensure that the value is in a certain range. Synopsis:

Mathf.Clamp(sourcevalue, min, max)

and it returns the clamped value. Examples:


int x = Mathf.Clamp(12, 10, 15); // x == 12, since it's in the range int y = Mathf.Clamp(17, 10, 15); // y == 15 (the max) int z = Mathf.Clamp(5, 10, 15); // z == 10 (the min)

// might look something like this:


// grab the current rotation in euler angles float RotationX = transform.eulerAngles.x; float RotationY = transform.eulerAngles.y; float RotationZ = transform.eulerAngles.z;

// assuming you've defined a float named RotationSpeed above RotationY += Input.GetAxis("Horizontal") * RotationSpeed;

// clamp the value to your MinRotation and MaxRotation values that you // defined above RotationY = Mathf.Clamp(RotationY, MinRotation, MaxRotation);

//assign the rotation back to the transform -- we can't assign it // back directly because the individual components are read-only transform.rotation = Quaternion.Euler(RotationX, RotationY, RotationZ);


As long as you do the clamp before the end of the frame (say, before you return from your Update() method), the player will never see the gun rotated outside of its limit. The drawback of doing it this way is that if you want to create some kind of feedback to tell the player that the gun won't rotate any more, you will need to write (a little) more code for that.

Hope this helps!

it sounds like your approach is:

  • get user input
  • rotate gun
  • check if current angle is too big
  • if yes, ignore further input

i think the better approach might be:

  • get user input
  • check if new angle is too big
  • if not, rotate gun
  • if yes, don't rotate gun

this way you're not ignoring user input and don't have to worry about not ignoring it once they let up on that key. you can also use that continued "bad" input to alert the player they've reached the turn limit on the gun by playing a small sound of some kind (gear grinding noise for example) to let them know they can't turn anymore.

guys are right about current problem but if you want to ignore input in any other situation you can define a bool variable in your component and when you want to ignore input set it to true and when you don't want to ignore set it to false. then in your input code check the status of that variable. see this small piece of C# code

if (input.GetKeyDown(KeyCode.left) && ignoreLeft == false)
//input code here