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 whats wrong. I have been stuck at this problem all day.
I have attached some screenshots so you can see how I have achieved this:
this is my animator>>>>>http://prntscr.com/bunpjn
this is my blend tree in the idle state>>>>>http://prntscr.com/bunpe5 and so far,this is my script:
using UnityEngine;
using System.Collections;
public class
PlayerDirectionAndShooting : MonoBehaviour {
Rigidbody2D rbody;
Animator anim;
// Use this for initialization
void Start () {
rbody = GetComponent<Rigidbody2D> ();
anim = GetComponent<Animator> ();
}
// Update is called once per frame
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);
};
}
}