Click here to Skip to main content
15,850,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

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: Unity problem 2 - YouTube[^] 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!



C#
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;

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

What I have tried:

I have tried this: 
however the person's code doesn't work, I tried messaging them but she/his hasn't replied in 4 weeks. Thank you.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900