- Home /
Character input key doesnt work
I want to control my character with input key,for example i wanna run my character when i press the w button,for this i add a run animation to my character and i write a simple script code but when i run the project Ethan(my character) doesnt move or doesnt idle,nothing happens,can anyone help?
public Rigidbody rb;
public float thrust;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
rb.AddForce (transform.forward * thrust);
if (Input.GetKeyDown ("w")) {
GetComponent<Animation>().Play("HumanoidRun");
}
If it doesn't run or neither idle, there may have errors somewhere else. Does the console output anything ?
Did you tried to replace GetComponent().Play("HumanoidRun"); with a debug message ?
Did you tried to do the same in the Update loop in order to be sure that it the instruction is read ?
I tried to replace GetComponent().Play("HumanoidRun"); and got an exception UnityEngine.Component.GetComponent()' cannot be inferred from the usage. Try specifying the type arguments explicitly
and i put a debug message in if control and i got this message the main thread can go into if control i cannot achieve this challange help more please
Answer by alebasco · Jun 12, 2015 at 09:06 PM
GetKeyDown when using a string is looking for a key as named in the Input manager of Unity.
http://docs.unity3d.com/ScriptReference/Input.html
By default, it will be called "Vertical" and is an axis. This is generally how you want to use the input, as an axis.
If you want to avoid the Unity input then you should be checking Input.GetKeyDown(KeyCode.W);
This will tell you the frame that the W key is pressed in. If you want to know if the button is being pressed, then just use GetKey(KeyCode.W)
I used if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.W)) { GetComponent<Animation>().Play("HumanoidRun"); }
but still nothing happens
Are you using an Animation component (NOT Animator - they are different components!), with an animation named "HumanoidRun"?
Yes I am using animation component and using an animation which is named HumanoidRun but I cannot understand what's wrong help please..
Your answer
Follow this Question
Related Questions
move enemy to old position of player 2 Answers
Move character up and down while animation runs 2 Answers
Slowly move a GameObject on 1 axis, then destroy it. 1 Answer
Objects move on play 3 Answers