-1

If i'm using like now GetKeyDown instead KeyDown pressing the key "o" does nothing. But if i'm using GetKey and then KeyCode.O when i press on the O key the character stop walking but i need to keep pressing the O key all the time if i release the key the character will continue walking.

What i want to do is once when i click once on the key O the character will stop walking even when releasing the key and then if i click on the key "p" it will resume the walking.

The reason i'm using the speed property for pause/continue the walking is that if i will make:

gameObject.GetComponent<Animator> ().Stop(); 

The character stop walking but then the rest of the scene/world is moving to the character. And when i tried to use:

gameObject.GetComponent<Animator> ().Play();

Then i need to put some parameter in the Play like Play("Walk") but it didn't work.

That's why i decided to use the speed property for pause/continue.

using UnityEngine;
using System.Collections;

public class Ai : MonoBehaviour {

    Animator _anim;
    // Use this for initialization
    void Start () {

        _anim = GetComponent<Animator> ();
    }


    // Update is called once per frame
    void Update () {

        if (Input.GetKey (KeyCode.Z)) {
            _anim.speed = 20.0f;
        }

        if (Input.GetKeyDown ("o"))
            gameObject.GetComponent<Animator> ().speed = 0;
        if(Input.GetKeyDown("p"))
            gameObject.GetComponent<Animator>().speed = 2;
    }
}
TheLost Lostit
  • 505
  • 6
  • 28

1 Answers1

1

Input.GetKeyX can only get keys that are specified under Edit > Project Settings > Input. You need to specify your key there if it does not match one of the existing ones.

Just change one of the existing ones if you don't need it or change the size to create an extra on. The name of the axis is what you put into Input.GetKeyX, for example Input.GetKeyDown("Fire1").

Edit:
Some more indepth information in the docs: https://docs.unity3d.com/Manual/ConventionalGameInput.html

Gunnar B.
  • 2,879
  • 2
  • 11
  • 17
  • Ok found how to do it. Not sure if it's the right way but it's working. In the Window > Animator i created before an empty state called it Walk and set it is default so the character always walking when running the game using the humanoidwalk. Now i added a new empty state called it Stop and using the Humanoididle and in the script i'm using: Input.GetKeyDown(KeyCode.O) and then _anim.Play ("Stop"); and it's working. Is that a good way to do it ? – TheLost Lostit Jul 26 '16 at 15:23
  • Sounds ok. Depends a bit on what you want. You can also make idle the default state. Also, you can make transitions in the animator and have them linked to variables you can create there. E.g. you can make a bool `isWalking` (or whatever) and make a `false -> true` transition from idle to walk. Then in your code you change the bool. Have a look at this tutorial https://unity3d.com/learn/tutorials/topics/animation/animator-controller?playlist=17099 and number 4 for how to set up an animator. – Gunnar B. Jul 26 '16 at 16:01