change animation to make character face a waypoint.
Hi.
I'm making a point and click game in 2D. My player is a sprite and has a different animation for walking north, east, south and west. The player checks each Update to see if there is a waypoint in the queue. If there is, he walks toward it. On reaching the destination the waypoint is deleted and he starts moving to the next one.
I'm struggling with determining which direction he is facing, so that I can change his animation. My initial thoughts were to determine the distance to the next waypoint on the X & Y. If X is greater than Y the character should walk horizontally, and if X > 0 he's moving east, X < 0 would be west etc.
The code I have does change the animation, but the player stutters and moonwalks like the ghost of Michael Jackson, so my logic is clearly incorrect.
This is the snippet I have so far:
case State.Walking:
Debug.Log(playerState);
//**DETERMINE DIRECTION OF SPRITESHEET, (N,E,S,W)
float distX = transform.position.x - wayPoints[0].x;
float distY = transform.position.y - wayPoints[0].y;
//East
if (distX > distY && distX >= 0)
{
animator.SetTrigger("walkEastWest");
sr.flipX = true;
}
//West
if (distX > distY && distX < 0)
{
animator.SetTrigger("walkEastWest");
sr.flipX = false;
}
//North
if (distX < distY && distY >= 0)
{
animator.SetTrigger("walkUp");
}
//South
if (distX < distY && distY < 0)
{
animator.SetTrigger("walkDown");
}
//**ANIMATE THE POSITION, SET AND REMOVE WAYPOINTS ETC
if (transform.position != destination) {
transform.position = Vector3.MoveTowards(transform.position, destination, step);
}
if (transform.position == destination) {
wayPoints.RemoveAt(0);
if (wayPoints.Count > 0){
destination = wayPoints[0];
//measure distance/direction
}
if (wayPoints.Count <= 0) {
playerState = State.Idle;
}
}
r.material.color = Color.red;
break;
I'm happy to post any further code/info needed to sort this out.
Thanks
Dan
Your answer
Follow this Question
Related Questions
How to implement gun animations 0 Answers
Animation which uses trigger is played twice before going back to the previous state. 0 Answers
Why StateMachineBehaviour not calling OnStateExit when doing animator.Crossfade through code? 0 Answers
How to replace an Animation State in Animator, without rebuilding all the transitions? 1 Answer