Hello!
Im trying to add extra animations like, sneak_idle,sneak_run,sneak_walk
And how i trying to do is if you hold E down, player sneak_idle, and if you push both W and E, player sneak_walk, and if you hold W - E - Ctrl down, player sneak_run
The thing is with my code. if you hold W -E down, player sneak_run, and that should only happen if you hold W - E - Ctrl down, why?
Heres the code i use, and if you have a better way to do this, tell me 
function Update () {
if ((Input.GetKey(KeyCode.RightControl) || Input.GetKey(KeyCode.E)) && Input.GetKey(KeyCode.W)){
animation.Play("sneak_run");
}
}
Right now it’s;
(RightControl OR E) AND W
So, it returns true if (RightControl AND W) and (E AND W).
So i should use “&&” between all inputs? i tried that but didnt work
Seams like i cant use same button to “build on” if i use this it do works, but this isnt what i like it to be
function Update () {
if (Input.GetKey (KeyCode.Q))
animation.Play("sneak_idle");
if (Input.GetKey (KeyCode.E) && Input.GetKey(KeyCode.W))
animation.Play("sneak_walk");
}
You should maybe implement a toggle for your sneak instead of holding so many input.
How do i toggle a simple button? cant find anything but GUI things
private bool isSneaking = false;
private void Update()
{
if (Input.GetKeyDown(KeyCode.E))
isSneaking = !isSneaking;
}
Im using Java and i cant convert at all lol. However, i tried this but cant get the button to act as it is toggle
#pragma strict
private var onoff : boolean = false;
function Update () {
if (Input.GetKey(KeyCode.E)){
onoff = !onoff;
if (onoff){
animation.CrossFade("sneak_idle");
} else {
animation.Stop("sneak_idle");
}
}
}
take the
if(onoff){...}
bit out of the button if.
if(Input.GetKey...)
{...
}
if(onoff)
{...
}
GetKey returns true every frames the user hold the key.
GetKeyDown return true only the first frame the user pressed the key.
So, i changed the codes to this as you 2 guys pointed out, still doesnt work, It must be something with the key input?
#pragma strict
private var onoff : boolean = false;
function Update () {
if (Input.GetKeyDown (KeyCode.E)){
onoff = !onoff;
if (onoff)
animation.CrossFade("sneak_idle");
} else {
animation.Stop("sneak_idle");
}
}