Ai sprite animation 8 directional movement
Hi guys,
I'm trying to have the AI in my game (assets are 2D, but in a 3D isometric world/level) animate sprites accordingly to the direction they are facing, on their own as they move around. I have used the following code by following a youtube tutorial, however with this code, the AI only changes sprites in response to key inputs with the Animator's Blend Tree:
private Animator anim;
public Vector3 speed = new Vector3(1, 0, 1);
// Use this for initialization
void Start()
{
anim = GetComponent<Animator>();
}
void Update()
{
float inputX = Input.GetAxis("Horizontal");
float inputY = Input.GetAxis("Vertical");
anim.SetFloat("SpeedX", inputX);
anim.SetFloat("SpeedY", inputY);
Vector3 movement = new Vector3(
speed.x * inputX,
speed.y * inputY,
0);
}
// Update is called once per frame
void FixedUpdate()
{
float lastInputX = Input.GetAxis("Horizontal");
float lastInputY = Input.GetAxis("Vertical");
if (lastInputX != 0 || lastInputY != 0)
{
anim.SetBool("Bounce", true);
if (lastInputX > 0)
{
anim.SetFloat("LastMoveX", 1f);
}
else if (lastInputX < 0)
{
anim.SetFloat("LastMoveX", -1f);
}
else
{
anim.SetFloat("LastMoveX", 0f);
}
if (lastInputY > 0)
{
anim.SetFloat("LastMoveY", 1f);
}
else if (lastInputY < 0)
{
anim.SetFloat("LastMoveY", -1f);
}
else
{
anim.SetFloat("LastMoveY", 0f);
}
}
else
{
anim.SetBool("Bounce", false);
}
}
How could I change this so that the AI is able to change the sprite animations on its own using the Blend Tree in the Animator, rather than using input keys.
Thanks for the help!
Hey, man! Can you share the youtube tutorial to me? I'm trying to make a top-down view 2d game. But there's only 4-way sprites tutorial no 8 ways. thank you very much~
Your answer
Follow this Question
Related Questions
How To Stop Enemy Movement During Its Attack Animation 1 Answer
Virtual camera odd behaviour. Scriptting noob questions 0 Answers
No Gravity Jump 0 Answers
Trouble making jumping spider enemies 0 Answers