0
\$\begingroup\$

I am making a horror game, and I want to press a key to look up while I am idle, that i've done but if I press the W key it stands in idle but floating forward. How can I disable the W key while I press the F key to look up ? Everything works fine I can press F and the LookUp Animation is being played and if KeyUp on F the Animation goes back to Idle. My problem is if I am in LookUp Animation when I press F, I can also press W to go forward but its floating not walking. I want to deactivate this function so I can just press F when I am Idle.

Thanks for help.

my Script:


public float speed = 10f;
public Rigidbody rgb;
public Animator anim;
bool walk = false;

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

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

    float move = Input.GetAxis ("Vertical");
    anim.SetFloat ("Speed", move);

    if (Input.GetKey (KeyCode.W) | walk == true) 
    {
        rgb.transform.position -= new Vector3(0, 0, 1) * speed * Time.deltaTime;
        Debug.Log("Walking");
    }
    else
    {
        walk = false;
        Debug.Log("Idle");
    }


    if (Input.GetKey (KeyCode.F)) 
    {
        anim.SetBool("LookUpIdle", true);
        rgb.transform.position = transform.position;
    }
    else
    {
        anim.SetBool("LookUpIdle", false);
    }


}   

}

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Mind enclosing your walk code in if clause to check wether the F key is held down. Also be sure to check the keyDown and keyUp events for triggering LookUp.

void Update()
{            
    float move = Input.GetAxis ("Vertical");
    anim.SetFloat ("Speed", move);
    if(!Input.GetKey(KeyCode.F))//Add this line to perform movement only if the F key isn't pressed
    {
        if (Input.GetKey (KeyCode.W) | walk == true) 
        {
            rgb.transform.position -= new Vector3(0, 0, 1) * speed * Time.deltaTime;
            Debug.Log("Walking");
        }
        else
        {
            walk = false;
            Debug.Log("Idle");
        }
    }

    if (Input.GetKeyDown (KeyCode.F)) 
    {
        anim.SetBool("LookUpIdle", true);
        rgb.transform.position = transform.position;
    }
    else if (Input.GetKeyUp (KeyCode.F)) 
    {
        anim.SetBool("LookUpIdle", false);
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Im confused, what code should I put in there ? Thanks for your answer :) \$\endgroup\$ Aug 28, 2016 at 16:32
  • \$\begingroup\$ @NewmakerCompany Sorry, adjusted to fit your code. I guess this way walking should be disabled while F is pressed \$\endgroup\$
    – Leggy7
    Aug 28, 2016 at 17:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .