Since, I am a new user, I can only put two up-links, so if you could click here to read the full information, thank you . 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:
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(transform.position == target)
{
anim.SetBool ("walking", false);
}
if (Input.GetMouseButtonDown (0))
{
Vector3 mousePosition = Input.mousePosition;
mousePosition.z = 10; // distance from the camera
target = Camera.main.ScreenToWorldPoint (mousePosition);
target.z = transform.position.z;
var movementDirection = (target - transform.position).normalized;
Vector3 animDirection = Vector3.zero;
// Use >= to default to horizontal on both being equal
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);
}
}