Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Unity 3D: Idle check direction

BakausagichanBakausagichan hayesMember Posts: 4

Basically I have created a 2d rpg click to move game on unity. My problem is that my player isn't facing the right direction once it has reached it's position, for example please take a look at this video: image Thank you.

private Animator anim;
public float speed = 15f;
private Vector3 target;
public cove playerMovementRef;
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;

        var movementDirection = (target - transform.position).normalized;

        if (movementDirection.x != 0 || movementDirection.y != 0) {
            anim.SetBool("walking" , true);
            anim.SetFloat("SpeedX" , movementDirection.x);
            anim.SetFloat("SpeedY" , movementDirection.y);

            if (movementDirection.x < 0) {
                anim.SetFloat("LastMoveX" , -1f);
            }
            else if (movementDirection.x > 0) {
                anim.SetFloat("LastMoveX" , 1f);
            }
            else {
                anim.SetFloat("LastMoveX" , 0f);
            }
            if (movementDirection.y > 0) {
                anim.SetFloat("LastMoveY" , 1f);
            }
            else if (movementDirection.y < 0) {
                anim.SetFloat("LastMoveY" , -1f);
            }
            else {
                anim.SetFloat("LastMoveY" , 0f);
            }
        }
    }
    else {
        if (Mathf.Approximately(transform.position.x, target.x) && Mathf.Approximately(transform.position.y, target.y)) {
            touched = false;
            anim.SetBool("walking" , false);
        }
        else {
            transform.position = Vector3.MoveTowards(transform.position , target , speed * Time.deltaTime);
        }
    }
}
Tagged:
Sign In or Register to comment.