Preventing Player movement and/or keyboard input

Hiya

I’m trying to stop keyboard input while a lengthy shooting animation occurs for my player character so that he doesn’t slide around on the ground during it if the player still tries to walk around.

I’ve been trying to use this code:

function DisableKey( key : KeyCode )
{
if( Event.current.keyCode == key ( Event.current.type == EventType.KeyUp || Event.current.type == EventType.KeyDown ) )
{
Event.current.Use();
}
}

Calling via:
DisableKey( KeyCode.W );

But i get the message:
NullReferenceException: Object reference not set to an instance of an object

and if i try calling it via:
DisableKey( Event.keyCode.W );

I get:
An instance of type ‘UnityEngine.Event’ is required to access non static member ‘keyCode’.

Could someone please explain what I am doing wrong here? Also other strategies for handling stopping player movement at appropriate moments would be welcome.

Thanks

Making a boolean would be easier, in my opinion.

var gameIsPaused : boolean = false;
var inputBlocked : boolean = false;

if (Input.GetButtonDown (“space”) gameIsPause == false inputBlocked == false)

Thanks for the reply but wouldn’t this code need to be inserted directly above the command I’m trying to prevent? As I’m trying to prevent movement and that lives inside the inbuilt character controller script I can’t go in and edit this?

Well, you are probably using Move() or SimpleMove() from the character controller. So, you just need to as the “if” statement before calling one of these functions and if the conditions are not met, you don’t call the movement function, and therefore, the character doesn’t react to the input.

I don’t understand. You have a character that have an animation, and you don’t want the player to move it while it does his animation, right ?

How do you move your character ? With code lines.

What could allow or forbid code lines to be executed ? Booleans and tests.

Did I miss something ?

Right, I thought all the movement controls we’re locked inside the Character Controller script, I’m working with the 3D platform tutorial and didn’t realise that actually they exist inside the third person controller script (embarrassing). I fully understand how If statements and booleans function.

The thirdpersoncontroller script has already got a isControllable variable set up so I’d like to switch this to false but via another script on a game object linked to the player controller. I can set the isControllable var to be static but I think I need to do more to alter it from where I am than just say:

isControllable = false;

How should I go about altering variables in a different script on a game object further up the hierarchy from the script I’m in?

Cheers

To search for an object : GameObject.Find

To search for an object with a tag : GameObject.FindWithTag

To search for a component related to a gameObject : GetComponent

To access a variable of a component : simply write his name.

So : GameObject.Find(“Game Object Name”).GetComponent(ScriptName).isControllable

You want to find the gameobject the script is on, use
GameObject.Find() or GameObject.FindWithTag() etc.

Then you want the specific script, use
GameObject.GetComponent() etc.

Then its just a case of using
foundScript.isControllable = false;

If you don’t want to find it every frame, keep a reference of it, and just do the search once in Start().

This stuff really needs a forum sticky, there is like 20 posts a day asking how to do it! :stuck_out_tongue:

That’s exactly what I needed thanks for the assistance.

As the objects are linked is there a more efficient way to do this avoiding a ‘search’, along the lines of transform.root.something instead or is it simply not going to effect my overhead? I appreciate at the moment what I’m working on is quite small but to get into good practice for similar scenarios with lots of this going on…

Transform.Find to find a child by its name.

Transform.root to find the highest parent of a gameObject in the hierarchy.

Cool

In case anyone else was following along, ultimately I ended up using:

transform.root.GetComponent(ThirdPersonController).isControllable = false;

Thanks again AkilaeTribe I love your chicken she’s fluffy and round.

Google > hen folkmanis

She wants to be friends with humans but they end up rejecting her because she is too fat and takes too much place (poor beast) :wink: