# Need Help With My Script And Animator. I am encountering a few bugs with it

Okay so I have created a player archer character which when he using the arrow keys, faces those directions and upon hitting space when in that direction, the bow attack animation is played.

The code works fine but there are some nerve-wrecking problems:

when I unpress an arrow key, the character always returns to his default direction and when I press space when no arrow key is pressed, the right bow attack animation is played and also when i press the up arrow key and the space bar, the up bow attack animation does not play and instead again,the right bow attack animation is played.

I have looked at everything again and again but I can't seem to find anything wrong. The animation's assigned are okay and the code is okay too. Don't know what's wrong. I have been stuck at this problem all day.

I have attached some screenshots so you can see how I have achieved this:

my animator here

my blend tree in the idle state is here and so far, this is my script:

public class PlayerDirectionAndShooting : MonoBehaviour
{
Rigidbody2D rbody;
Animator anim;

void Start ()
{
rbody = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}

void Update ()
{
Vector2 direction_vector = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
anim.SetFloat("input_x", direction_vector.x);
anim.SetFloat("input_y", direction_vector.y);

if (Input.GetKeyDown("space"))
{
if (anim.GetFloat("input_x") <= -1)
{
anim.SetBool("is_shooting_left", true);
}
}
else
{
anim.SetBool("is_shooting_left", false);
}

if (Input.GetKeyDown("space"))
{
if (anim.GetFloat("input_y") <= -1)
{
anim.SetBool("is_shooting_down", true);
}
}
else
{
anim.SetBool("is_shooting_down", false);
}

if (Input.GetKeyDown("space"))
{
if (anim.GetFloat("input_y") <= 1)
{
anim.SetBool("is_shooting_up", true);
}
}
else
{
anim.SetBool("is_shooting_up", false);
}

if (Input.GetKeyDown("space"))
{
if (anim.GetFloat("input_x") <= 1)
{
anim.SetBool("is_shooting_right", true);
}
}
else
{
anim.SetBool("is_shooting_right",false);
}
}
}

• Can you please format your code? It's very hard to read it – Bálint Jul 19 '16 at 5:35
• Okay,sorry!!!!Let me fix it. – Muhammad Mahad Jul 19 '16 at 5:45
• Is it okay now?(Thanks for the edit smkplus!) – Muhammad Mahad Jul 19 '16 at 5:53
• "Here is my code, fix the bugs" type of questions are not good. Try to ask "how to do", not "what did I do wrong". Even If you ask a bug solving question, try to shrink your question into readable and easily understandable boundaries. – S. Tarık Çetin Jul 27 '16 at 16:59

I rearrange code here(for the jump and attack part):

         if (Input.GetKeyDown("space"))
{
if (anim.GetFloat("input_x") < 0)
{
anim.SetBool("is_shooting_left", true);
anim.SetBool("is_shooting_right", false);
}
if (anim.getFloat("input_x") > 0)
{
anim.SetBool("is_shooting_left", false);
anim.SetBool("is_shooting_right", true);
}
if (anim.GetFloat("input_y") < 0)
{
anim.SetBool("is_shooting_down", true);
anim.SetBool("is_shooting_up", false);
}
if (anim.GetFloat("input_y") > 0)
{
anim.SetBool("is_shooting_down", false);
anim.SetBool("is_shooting_up", true);
}
}


it should work.

• having multiple if (GetKeyDown("space")) is wrong.
• it's better to compare anim.GetFloat(input_x"or y") with 0. not 1 and -1 ( i don't know why you did that)
• But now the shooting animation keeps looping. – Muhammad Mahad Jul 19 '16 at 18:28
• Oh nevermind.I fixed that with this: else { anim.SetBool("is_shooting_down", false); anim.SetBool("is_shooting_left", false); anim.SetBool("is_shooting_right", false); anim.SetBool("is_shooting_up", false); } – Muhammad Mahad Jul 19 '16 at 18:32