I have a very simple sprite character animation only has three frames two are for a walk animation and a single standing sprite for when the character is not moving.
I’m trying to make a simple moment script that we can call to from other code to move the character so that when he is moving his moving animation play and when he is not moving the standing still animation plays.
using UnityEngine;
using System.Collections;
public class Character01Input : MonoBehaviour {
//Reference to the Character 01 Game Object
public GameObject FigureOne;
//animator reference
private Animator Fig01Ani;
public float speed;
void Start(){
Fig01Ani = FigureOne.GetComponent<Animator>();
//disable it on start to stop it from playing the default animation
Fig01Ani.enabled = true;
}
void Update () {
if (Input.GetKeyDown (KeyCode.A)) {//Start
//Fig01Ani.enabled = true;
transform.Translate (Vector2.right * speed);
Fig01Ani.SetBool ("Fig01MoveAni", true);
Fig01Ani.SetBool ("Fig01IdleAni", false);
//SFXmanager SFXfw = FindObjectOfType<SFXmanager> ();
//SFXfw.FigWalkSFX ();
Fig01Ani.Play ("Fig01MoveAni");
}
if (Input.GetKeyUp (KeyCode.A)) {//Stop
//Fig01Ani.enabled = true;
Fig01Ani.SetBool ("Fig01MoveAni", false);
Fig01Ani.SetBool ("Fig01IdleAni", true);
//transform.Translate (Vector2.right * speed);
//SFXmanager SFXfw = FindObjectOfType<SFXmanager> ();
//SFXfw.FigWalkSFX ();
Fig01Ani.Play ("Fig01IdleAni");
}
if (Input.GetKeyDown (KeyCode.D)) {//Start
//Fig01Ani.enabled = true;
transform.Translate (-Vector2.right * speed);
Fig01Ani.SetBool("Fig01MoveAni", true);
Fig01Ani.SetBool("Fig01IdleAni", false);
transform.Translate (-Vector2.right * speed);
//SFXmanager SFXfw = FindObjectOfType<SFXmanager> ();
//SFXfw.FigWalkSFX ();
Fig01Ani.Play ("Fig01MoveAni");
}
if (Input.GetKeyUp (KeyCode.D)) {//Stop
//Fig01Ani.enabled = true;
Fig01Ani.SetBool("Fig01MoveAni", false);
Fig01Ani.SetBool("Fig01IdleAni", true);
//transform.Translate (-Vector2.right * speed);
//SFXmanager SFXfw = FindObjectOfType<SFXmanager> ();
//SFXfw.FigWalkSFX ();
Fig01Ani.Play ("Fig01IdleAni");
}
if (Input.GetKeyDown (KeyCode.S)) {//start
//Fig01Ani.enabled = true;
transform.Translate (Vector2.up * speed);
Fig01Ani.SetBool("Fig01MoveAni", true);
Fig01Ani.SetBool("Fig01IdleAni", false);
transform.Translate (Vector2.up * speed);
//SFXmanager SFXfw = FindObjectOfType<SFXmanager> ();
//SFXfw.FigWalkSFX ();
Fig01Ani.Play ("Fig01MoveAni");
}
if (Input.GetKeyUp (KeyCode.S)) {//Stop
//Fig01Ani.enabled = true;
Fig01Ani.SetBool("Fig01MoveAni", false);
Fig01Ani.SetBool("Fig01IdleAni", true);
//transform.Translate (Vector2.up * speed);
//SFXmanager SFXfw = FindObjectOfType<SFXmanager> ();
//SFXfw.FigWalkSFX ();
Fig01Ani.Play ("Fig01IdleAni");
}
if (Input.GetKeyDown (KeyCode.W)) {//Start
//Fig01Ani.enabled = true;
transform.Translate (-Vector2.up * speed);
Fig01Ani.SetBool("Fig01MoveAni", true);
Fig01Ani.SetBool("Fig01IdleAni", false);
transform.Translate (-Vector2.up * speed);
//SFXmanager SFXfw = FindObjectOfType<SFXmanager> ();
//SFXfw.FigWalkSFX ();
Fig01Ani.Play ("Fig01MoveAni");
}
if (Input.GetKeyUp (KeyCode.W)) {//Stop
//Fig01Ani.enabled = true;
Fig01Ani.SetBool("Fig01MoveAni", false);
Fig01Ani.SetBool("Fig01IdleAni", true);
//transform.Translate (-Vector2.up * speed);
//SFXmanager SFXfw = FindObjectOfType<SFXmanager> ();
//SFXfw.FigWalkSFX ();
Fig01Ani.Play ("Fig01IdleAni");
}
}
}