How do I create the animator and the character controller for this situation?

How can i create a character control and animator for this sprite sheet that when i press the arrow keys,the player faces in that direction but does not move and when i press the space bar,the bow attack animation is played and a projectile is launched?

This may be a tall order but please help me out here!I don’t know how to start working this out!

There isn't really anything tricky to this - with just a basic understanding of the animation system you should be able to figure this out. I suggest you go through some of the tutorials on the animation system here: http://unity3d.com/learn/tutorials/topics/animation Those should give you everything you need fairly quickly.

1 Answer

1

Okay I’ll try to give you some guidance here. First, you’d setup the Animation controller kind of like this:

Each of the transitions to the directional states has a condition set so that it only moves when the relevant bool is set and shooting is false. The transition to shoot happens whenever shoot is true.

Then, I’d have two separate scripts. The first script will set/unset movement variables as needed, and the second will read the input. Here they are:

public class AnimController : MonoBehaviour{
    private string m_LastDirection;
	private Animator m_Anim;
	
	private void Start(){
		m_Anim = GetComponent<Animator>();
	}
	
	public void SetDirection(string dir){
		//Dont do anything if the animation is already set for that direction
		if (dir != m_LastDirection){
			//Set all movement bools to false
		    m_Anim.SetBool("left", false);
			m_Anim.SetBool("right", false);
			m_Anim.SetBool("up", false);
			m_Anim.SetBool("down", false);
			
			//Move in the given direction
			m_Anim.SetBool(dir, true);
			
			//Save which direction we were last set to move in
			m_LastDirection = dir;
		}
	}
	
	public void SetShoot(bool val){
	    m_Anim.SetBool("shoot", val);
	}
}

public class Character : MonoBehaviour{
	private AnimController m_AnimController;
	private float m_ShootingTime; //This is the time the shooting animation takes
	private void Start(){
		m_AnimController = GetComponent<AnimController>();
	}
	
	private void Update(){
		if (Input.GetKeyDown(KeyCode.Left)){
			m_AnimController.SetDirection("left");
		}
		if (Input.GetKeyDown(KeyCode.Up)){
			m_AnimController.SetDirection("up");
		}
		if (Input.GetKeyDown(KeyCode.Right)){
			m_AnimController.SetDirection("right");
		}
		if (Input.GetKeyDown(KeyCode.Down)){
			m_AnimController.SetDirection("down");
		}
		if (Input.GetKeyDown(KeyCode.Space)){
			m_AnimController.SetShoot(true);
			StartCoroutine(WaitForShoot())
		}
	}
	
	private IEnumerator WaitForShoot(){
		yield return new WaitForSeconds(m_ShootingTime);
		m_AnimController.SetShoot(false);
	}
}

Note that this code is not tested, but it should help get you started.

Thanks for the reply! Shouldn't the entry state be connected to the any state though?