- Home /
Scripting "If only" functions?
The title may be confusing but it's not wrong. I wrote a script to deal with animations for sprinting in my game. So A is walk, D is walk, Shift+A is run, Shift+D is run. The problem is unity sees Shift+A as Shift and A, so it does the walk function rather than the run function. I got to this point but couldn't actually solve the problem :D typical noob. Heres the script you can see what im talking about` var animator : Animator;
function Update(){
if(Input.GetKey(KeyCode.A) && Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.LeftShift)){
animator.SetBool("Running", true);
}else{
animator.SetBool("Running", false);
}
if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D)){
animator.SetBool("Walking", true);
}else{
animator.SetBool("Walking", false);
}
}
thank you robert i don't know why the code thing wasn't working
Answer by Kiwasi · May 13, 2014 at 03:50 AM
Simple solution. Line 7 should employ the not operator !
if(Input.GetKey(KeyCode.A) && !Input.GetKey(KeyCode.LeftShift)){
// You can do the same thing with an or for the D key
You could also use a nested if to achieve the same function.
if(Input.GetKey(KeyCode.A)){
if(Input.GetKey(KeyCode.LeftShift)){
//Do stuff that needs both buttons pushed
} else {
//Do stuff that only relys on A
}
}
Answer by shraa1 · May 13, 2014 at 04:05 AM
how about try select case?
switch(Input.GetKey)
{
case KeyCode.LeftShift:
if(Input.GetKey(KeyCode.A)||Input.GetKey(KeyCode.D))
//run animation script
case default:
//walk animation script
}
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Enemy AI Animation 0 Answers
Attack animation Wont Play 1 Answer
Why won't it run 1 Answer