I'm having a problem with returning back to my idle state animation in my game. This
video[
^] is an example of what my problem is. My player doesn't go back to idle state animation, which I did try adding this to my code:
anim.SetBool ("walking", false);
(This is placed near to the end of my
code[
^])
But this
happens[
^]. Which is not what I want. In the
first[
^] video I showed you, it shows me clicking the
walking parameter off[
^] so that I can show you what my animation would look like if my player stop once it reached it's destination.In the video you can see that my player faced the wrong way after walking down and up, it faced this
way[
^] and that
way[
^], which I would also like to fix, but have no idea how to. So can anyone help me with both of my problems, which is:
Returning back to idle animation once my player reaches it's destination.
And making sure that my player face the correct direction as it is suppose to. My player's sprite
image[
^]
Here is my code:
<pre lang="C#">
private Animator anim;
public float speed = 15f;
private Vector3 target;
private bool touched;
private bool playerMovementRef;
void Start () {
target = transform.position;
anim = GetComponent<Animator> ();
}
void Update () {
if (Input.GetMouseButtonDown (0)) {
Vector3 mousePosition = Input.mousePosition;
mousePosition.z = 10;
target = Camera.main.ScreenToWorldPoint (mousePosition);
target.z = transform.position.z;
var movementDirection = (target - transform.position).normalized;
Vector3 animDirection = Vector3.zero;
if (movementDirection.x > movementDirection.y)
animDirection.x = 1;
else
animDirection.y = 1;
anim.SetBool ("walking", true);
anim.SetFloat ("SpeedX", movementDirection.x);
anim.SetFloat ("SpeedY", movementDirection.y);
Debug.LogFormat ("X: {0}, Y: {1}", movementDirection.x, movementDirection.y);
if (movementDirection.x > 0) {
anim.SetFloat ("LastMoveX", 1f);
} else if (movementDirection.x < 0) {
anim.SetFloat ("LastMoveX", -1f);
} else {
if (movementDirection.y > 0) {
anim.SetFloat ("LastMoveY", 1f);
} else if (movementDirection.y < 0) {
anim.SetFloat ("LastMoveY", -1f);
} else {
anim.SetFloat ("LastMoveY", 0f);
}
}
} else {
transform.position = Vector3.MoveTowards (transform.position, target, speed * Time.deltaTime);
}
}
}
What I have tried:
I have tried adding this into my code (in update) but this happen
happen[
^]
if(transform.position == target)
{
anim.SetBool ("walking", false);
}