- Home /
Crazy weird input script order glitch please help!
Unity will ONLY handle one of either my "walkLeft" or "walkRight" animations, depending on which one comes LAST in the order of the code:
So if I put this:
if ( Input.GetButton("left") ) {
dude.animation["walkLeft"].speed = 1;
dude.animation.CrossFade("walkLeft");
print("left");
}
if ( Input.GetButton("right") ) {
dude.animation["walkRight"].speed = 1;
dude.animation.CrossFade("walkRight");
print("right");
}
then "walkRight" will work when "right" is pressed, but "walkLeft" WILL NOT WORK when "left" is pressed.
Or, alternately, if I put this:
if ( Input.GetButton("right") ) {
dude.animation["walkRight"].speed = 1;
dude.animation.CrossFade("walkRight");
print("left");
}
if ( Input.GetButton("left") ) {
dude.animation["walkLeft"].speed = 1;
dude.animation.CrossFade("walkLeft");
print("right");
}
then "walkLeft" will work when "left" is pressed, but "walkRight" WILL NOT WORK when "right" is pressed.
print("right"); and print("left"); always work regardless of the order. What am I doing wrong here?!?! I'm banging my head against the wall trying to figure this out.
Full code:
function FixedUpdate () {
if( lockControls == false) {
if (Input.GetButtonUp("Fire1")){
idling = false;
lockControls = true;
cleanSound.PlayOneShot(cleanSound.clip);
dudeControl.animation.Stop("guy_walk_sounds");
dude.animation.CrossFade("clean");
controlUnlock();
}
if ( Input.GetButton("left") ) {
dude.animation["walkLeft"].speed = 1;
dude.animation.CrossFade("walkLeft");
print("left");
}
if ( Input.GetButton("right") ) {
dude.animation["walkRight"].speed = 1;
dude.animation.CrossFade("walkRight");
print("right");
}
else{
dude.animation["walkLeft"].speed = 0;
dude.animation["walkRight"].speed = 0;
dudeControl.animation.Stop("guy_walk_sounds");
idling = true;
Idle();
}
var keyboardX = Input.GetAxis("Horizontal") * keyboardSpeed * Time.deltaTime;
var newPos = rigidbody.position + Vector3( (-keyboardX*walkDirec), 0, 0);
if (newPos.x < -1.66){
newPos.x = -1.66;
}
else if (newPos.x > 1.52){
newPos.x = 1.52;
}
rigidbody.MovePosition(newPos); // Move the object.
}
}
Answer by SilverTabby · Jul 24, 2011 at 09:44 PM
This might not be the source of the problem but here's what I'm seeing:
if (Input.GetButtonUp("Fire1")){
//...
}
if ( Input.GetButton("left") ) {
//...
}
if ( Input.GetButton("right") ) {
//...
}
else{
//...
}
Why do you only have 1 else for the entire block of ifs? If you are doing what I think you're trying to do, you need to this (one else per if):
if (Input.GetButtonUp("Fire1")){
//...
}
else if ( Input.GetButton("left") ) {
//...
}
else if ( Input.GetButton("right") ) {
//...
}
else{
//...
}
I'm unsure if that will solve your problem, but it might.
Yes, that is the problem. Thanks! I didn't know about "else if" statements.
You are a winner! This works now!
That was seriously driving me crazy, I just had no idea what to even search for to try to learn how to fix this code.
There is no "else if" statement, its just a clear way of writing it. It is the same as:
if (Input.GetButtonUp("Fire1")){
//...
} else
if ( Input.GetButton("left") ) {
//...
} else
if ( Input.GetButton("right") ) {
//...
} else {
//...
}
SilverTabby's formatting is the better way to write it of course, but it is worth understanding exactly what you are doing.
Here's a nice tutorial on if-else statements
http://download.oracle.com/javase/tutorial/java/nutsandbolts/if.html
Your answer
Follow this Question
Related Questions
How to animate two animation clips at the same time? 1 Answer
GetButton Problem 1 Answer
Animation problem in script 0 Answers
Problem with animations, How to blend them?? 0 Answers
Animation Issue 2 Answers