Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

No input with Input.GetKey()

Discussion in 'Scripting' started by The-Game-Master, Aug 6, 2014.

  1. The-Game-Master

    The-Game-Master

    Joined:
    Aug 6, 2014
    Posts:
    54
    using UnityEngine;
    using System.Collections;

    public class WeaponAnimControl : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {
    Animator anim = GetComponent<Animator>();
    Animation anima = GetComponent<Animation>();
    int RightClick = Animator.StringToHash ("RightClick");
    if(Input.GetKey (KeyCode.Mouse2)){
    anim.SetBool(RightClick, true);
    anima.Play ("aimdownsights");
    Debug.Log ("Aiming Down Sights");
    }
    else{
    anim.SetBool(RightClick, false);
    anima.Play ("idle");
    Debug.Log ("Idle-ing");
    }
    }
    }

    When I right click, neither the Bool or the animation play. In addition, the console doesn't print anything out.
    What am I doing wrong?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    It's not made expressly clear in the docs, but I'm pretty sure GetKey only works for keyboard input. Try Input.GetMouseButton or Input.GetButton instead.
     
  3. The-Game-Master

    The-Game-Master

    Joined:
    Aug 6, 2014
    Posts:
    54
    Thank you, but it still won't work.
    I can't understand what I'm doing wrong. Also, my gun disappeared when I play now.
     
  4. The-Game-Master

    The-Game-Master

    Joined:
    Aug 6, 2014
    Posts:
    54
    Why is my gun teleporting?
    I use an animation, and the gun just teleports.
    I'm not understanding. it's attatched to the Main Camera in the First Person Controller.
    Why does it teleport way way waayyyy out in space?
     
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Turn off root motion in your animation settings.
     
  6. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
  7. The-Game-Master

    The-Game-Master

    Joined:
    Aug 6, 2014
    Posts:
    54
    Thank you all. I WAS GETTING SO ANNOYED but now I got it. Thanks so much!
     
  8. The-Game-Master

    The-Game-Master

    Joined:
    Aug 6, 2014
    Posts:
    54
    Now I just need to figure out how to mark this as "Solved".