stop forward movement

Hi,

If my fps player hits an object (fps with an object sticking out of it) I want to stop it moving in that direction.
How do I disable the forward key but allow backward movement to happen?

	Debug.DrawRay(transform.position, fwd * 0.2f, Color.green);
	 if(Physics.Raycast(transform.position, fwd,out hit, 0.2f)){
	   
		if(hit.collider.gameObject.name == "Enemy" || hit.collider.gameObject.name == "Wall"){
		   //cant move forward
		}
	 }

you could do something like this:

private string text = "I bet you can't type an A!";



void DisableKey( KeyCode key )

{

    if( Event.current.keyCode == key && ( Event.current.type == EventType.KeyUp || Event.current.type == EventType.KeyDown ) )

    {

        Event.current.Use();

    }

}



void OnGUI()

{

    DisableKey( KeyCode.A );

    text = GUILayout.TextArea( text );

}

The easiest way is to use a CharacterController in your character, and move it with Move - this will manage collisions automatically, and your character will never go through things that have colliders (as @DGArtistsInc said). There’s a good example script in the docs: http://unity3d.com/support/documentation/ScriptReference/CharacterController.Move.html

But if your character is a simple object moved with Translate and you don’t want to use a CharacterController (not a good idea), it’s better to disable the movement, not the forward key - edit your question and post your movement code, so we can suggest the necessary changes (but you’d better to use a CharacterController!)