Howdy, Stranger!

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

Categories

Unity 3D: Improving Idle check direction

BakausagichanBakausagichan hayesMember Posts: 4

I have a 2d RPG click to move game. My only problem I have now is my animation. Below is the code for my animation and here is a link: image to a video regarding the problem of my well problem. Does anyone know how to improve this code so that my animation and look smooth whilst it's moving and it doesn't face the wrong direction once it's reached it's destination (if you don't understand what I mean, I strongly recommend you watch the video). Thank you!

private Animator anim;

public float speed = 15f;

private Vector3 target;

private bool touched;

void Start () {
target = transform.position;
anim = GetComponent ();
}

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);
}
}

Sign In or Register to comment.