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.
}
}