- Home /
2D Character Animation Help
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");
}
}
}
What is your question? If you have set up your animation state transitions, I can't see why your code wouldn't work as intended.
@Cherno, Thanks I had to pull out the actual direction on the get get button down. these were just done as a test anyways but I actually want to call the moments from another script which would call something like this:
//UP $$anonymous$$ovement Direction Control
public void COne_DirectionUP(){
Fig01Ani = FigureOne.GetComponent<Animator>();
FigureOne.transform.Translate (Vector2.up * speed);
//SFXmanager SFXfw = FindObjectOfType<SFXmanager> ();
//SFXfw.FigWalkSFX ();
Fig01Ani.Play ("Fig01$$anonymous$$oveAni");
}
Thing is that it's playing the animation but not moving my object in that direction?
Try
FigureOne.transform.Translate (Vector2.up * Time.deltaTime * speed);
Also make sure this code is called every frame (GetButton), not just once (as it would be with GetButtonDown).
Answer by slametnhd · Mar 17, 2016 at 03:06 PM
I don't help you about your code, you must put image animation controller configuration, may can help you https://youtu.be/1qu_cehdASk