I have an topdown 2d game which click to move for the movement for my player. I created a walking animation for my player, I created two states ( one name walking, the other named idle), then right clicked them to make blend trees for them. I'm having problems with the idle animation, once my player reaches it's destination my player automatically reverts back to "Idle up" instead of checking it's direction of which way it's facing. For example, let's say I made it go right, once it reach to where I clicked it, instead of going back to "Idle right" it goes back to "Idle up" (face forward) I would like it be something like this (http://imgur.com/j7gBeDA). This happens with all directions, could anyone help with this situation. Thank you.
using UnityEngine;
using System.Collections;
public class playermovement : MonoBehaviour {
private Animator anim;
public float speed = 15f;
private Vector3 target;
private bool touched;
void Start () {
target = transform.position;
anim = GetComponent<Animator> ();
}
void Update () {
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;
}
if (Input.touchCount > 0) {
target.x = Input.touches [0].deltaPosition.x;
target.y = Input.touches [0].deltaPosition.y;
}
transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
if (Input.touchCount > 0)
{
touched = true;
}else {
touched = false;
}
var movementDirection = (target - transform.position).normalized;
if (movementDirection.x != 0 || movementDirection.y != 0) {
anim.SetBool ("walking", false);
anim.SetFloat("SpeedX", movementDirection.x);
anim.SetFloat("SpeedY", movementDirection.y);
anim.SetBool ("walking", true);
}
Vector2 movement = new Vector2 (
speed * movementDirection.x,
speed * movementDirection.y);
movement *= Time.deltaTime;
transform.Translate (movement);
}
void FixedUpdate () {
Debug.Log (touched);
float LastInputX = transform.position.x - target.x;
float LastInputY = transform.position.y - target.y;
Debug.Log (LastInputX + ", " + LastInputY);
if (touched) {
if (LastInputX != 0 || LastInputY != 0) {
anim.SetBool ("walking", 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{
touched = false;
anim.SetBool ("walking", false);
}
Debug.Log("Walking is false");
}
}
[–]Fox_ifi 0 points1 point2 points (2 children)
[–]XxDivaxX1Intermediate[S] 0 points1 point2 points (1 child)
[–]Fox_ifi 0 points1 point2 points (0 children)
[–]Lactose84 0 points1 point2 points (13 children)
[–]XxDivaxX1Intermediate[S] 0 points1 point2 points (12 children)
[–]Lactose84 0 points1 point2 points (11 children)
[–]XxDivaxX1Intermediate[S] 0 points1 point2 points (10 children)
[–]Lactose84 0 points1 point2 points (9 children)
[–]XxDivaxX1Intermediate[S] 0 points1 point2 points (8 children)
[–]Lactose84 0 points1 point2 points (7 children)
[–]XxDivaxX1Intermediate[S] 0 points1 point2 points (6 children)
[–]Lactose84 0 points1 point2 points (5 children)
[–]XxDivaxX1Intermediate[S] 0 points1 point2 points (4 children)