1
\$\begingroup\$

I have been following this tutorial. You can watch 16:00 - 18:20 to see what I am trying to do.

What should be happening is: I press a key, my animation plays and the parameters or Inputs get updated depending on which key I press. There are no errors in my script, but the parameters or Inputs are not updating. I have checked that I am using floats and that all my names match up. I've watched this part of the video and checked that my code was exactly like his multiple times. When using the model that he used in the video, the inputs the inputs are working fine. So would this mean there is something wrong with my animation, or is this an issue in my script?

using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour
{
    public Animator anim;

    private float inputH;
    private float inputV;


    // Use this for initialization
    void Start()
    {
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown("1"))
        {
            anim.Play("Armature|Walking", -1, 0f);
        }
        if (Input.GetKeyDown("2"))
        {
            anim.Play("Armature|TurnRight", -1, 0f);
        }
        if (Input.GetKeyDown("3"))
        {
            anim.Play("Armature|TurnLeft", -1, 0f);
        }
        if (Input.GetMouseButtonDown(0))
        {
            int n = Random.Range(0, 2);

            if (n == 0)
            {
                anim.Play("Armature|Walking", -1, 0f);
            }
            else
            {
                anim.Play("Armature|TurnRight", -1, 0f);
            }
        }
        inputH = Input.GetAxis("Horizontal");
        inputV = Input.GetAxis("Vertical");

        anim.SetFloat("inputH", inputH);
        anim.SetFloat("inputV", inputV);
    }
}

Anyone got any ideas?

\$\endgroup\$
7
  • \$\begingroup\$ Can we see your input manager? \$\endgroup\$
    – Gnemlock
    Dec 21 '16 at 0:30
  • \$\begingroup\$ Hey again lol!! \$\endgroup\$ Dec 21 '16 at 1:44
  • \$\begingroup\$ I added the input manager. Let me know if you need anything else. \$\endgroup\$ Dec 21 '16 at 1:48
  • \$\begingroup\$ Thus ones an easy fix. I brought this up before, but please consider going through the tutorials on the Unity website, first. This is the only way you will have suitable understanding of how core mechanics work - and they really are quite useful! \$\endgroup\$
    – Gnemlock
    Dec 21 '16 at 1:53
  • 1
    \$\begingroup\$ I'm asking this a lot these days but have you tried basic debugging? Your code works as well as it should, apart from the animator class which we have no knowledge in terms of how you're handling it. Try Debug.Log("This input works.") in the first input and test it that way. \$\endgroup\$ Dec 21 '16 at 7:40
0
\$\begingroup\$

Your using Input.GetKeyDown(string). If you check the API, you will note that the string should correspond to a key in the Input Manager.

In the Input Manager, you use names such as "Horizontal" and "Vertical". You make no reference to " 1" or "2". When you call Input.GetKeyDown, you should use the exact names as they are printed, or alternatively, use a KeyCode.

If you intend to use the Input Manager, look for input with commands such as Input.GetKeyDown("Horizontal") or Input.GetKeyDown("Vertical"). Note that it is case sensitive. Input.GetKeyDown("vertical") will not trigger input on a key labelled " vertical".

If you intend to reference keys, directly, use KeyCode. Every key on the keyboard has an associated KeyCode enumeration value; KeyCode.Alpha1 refers to the 1 button above the letters, for example, while KeyCode. Keypad1 refers to the 1 button in your numeric layout, if your keyboard has one.

\$\endgroup\$
8
  • \$\begingroup\$ Will try to improve this answer when I have the software available. \$\endgroup\$
    – Gnemlock
    Dec 21 '16 at 2:02
  • \$\begingroup\$ GetKeyDown actually has a fixed set of strings it will use (see the heading "Keys" at the bottom of this manual page), including values like "1" as described in the question. You may be thinking of GetButtonDown, which does use the names defined in the Input Manager. \$\endgroup\$
    – DMGregory
    Dec 21 '16 at 5:39
  • \$\begingroup\$ @DMGregory, are you sure? I interpret that excerpt to be detailing the format you would enter them into the Input Manager, not as a list of alternate name conventions. The API specifically notes that the string format should match with an axis in the Input Manager \$\endgroup\$
    – Gnemlock
    Dec 21 '16 at 6:11
  • 1
    \$\begingroup\$ The API says "For the list of key identifiers see Input Manager." where input manager is a link to the root of the input documentation - so I think they meant "see this documentation page" not "look at the inputs configured in the editor's input manager." Bad wording, as is so often the case with those docs. ;) I just tested to be sure, using "space" (one of the named keys from the list I linked) and "Jump" (one of the default Input Manager entries, mapped to space). GetKeyDown responds to "space" correctly, and throws an ArgumentException complaining it doesn't know what "Jump" is. \$\endgroup\$
    – DMGregory
    Dec 21 '16 at 6:21
  • \$\begingroup\$ I'm having a bit of trouble following this convo. I'm just getting into Unity animation and there was nothing in the video that mentioned input manager and it worked fine for him. Later today I will be trying again using the same model that he is using. BUT, is the consensus now that I don't need to update the input manager? Again, having some trouble following this convo. \$\endgroup\$ Dec 21 '16 at 19:54
-4
\$\begingroup\$

Here is the original text, with which I've compared, up until now.

using UnityEngine;
using System.Collections;

public class player : MonoBehaviour  
{
    public Animator anim;
    public Rigidbody rbody;

    private float inputH;
    private float inputV;
    private bool run;

    // Use this for initialization
    void Start () 
    {
        anim = GetComponent<Animator>();
        rbody = GetComponent<Rigidbody>();
        run = false;
    }

    // Update is called once per frame
    void Update () 
    {
        if(Input.GetKeyDown("1"))
        {
            anim.Play("WAIT01",-1,0f);
        }
        else if(Input.GetKeyDown("2"))
        {
            anim.Play("WAIT02",-1,0f);
        }
        else if(Input.GetKeyDown("3"))
        {
            anim.Play("WAIT03",-1,0f);
        }
        else if(Input.GetKeyDown("4"))
        {
            anim.Play("WAIT04",-1,0f);
        }

        if(Input.GetMouseButtonDown(0))
        {
            int n = Random.Range(0,2);

            if(n == 0)
            {
                anim.Play ("DAMAGED00",-1,0f);
            }
            else
            {
                anim.Play ("DAMAGED01",-1,0f);
            }
        }

        if(Input.GetKey(KeyCode.LeftShift))
        {
            run = true;
        }
        else
        {
            run = false;
        }

        if(Input.GetKey(KeyCode.Space))
        {
            anim.SetBool("jump",true);
        }
        else
        {
            anim.SetBool("jump", false);
        }

        inputH = Input.GetAxis ("Horizontal");
        inputV = Input.GetAxis ("Vertical");

        anim.SetFloat("inputH",inputH);
        anim.SetFloat("inputV",inputV);
        anim.SetBool ("run",run);

        float moveX = inputH*20f*Time.deltaTime;
        float moveZ = inputV*50f*Time.deltaTime;

        if(moveZ <= 0f)
        {
            moveX = 0f;
        }
        else if(run)
        {
            moveX*=3f;
            moveZ*=3f;
        }

        rbody.velocity = new Vector3(moveX,0f,moveZ);

    }
}

Here is a [link]1 for the complete project.

\$\endgroup\$
2
  • \$\begingroup\$ Thus might be helpful, but it does not attempt to answer the actual question. Perhaps this would have been better, as a comment? \$\endgroup\$
    – Gnemlock
    Dec 21 '16 at 2:03
  • \$\begingroup\$ I appreciate the help, but all this info is in the youtube video that I already linked. \$\endgroup\$ Dec 21 '16 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.