Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Click to move doh.

Discussion in 'Animation' started by soloRPG, Mar 16, 2024.

  1. soloRPG

    soloRPG

    Joined:
    May 24, 2021
    Posts:
    3
    At some point last night I seem to have broken my click-to-move script. My animator is set up properly with transitions. The player moves (translates) correctly but will not return to his idle animation from the walk once he stops. The walk continues to loop for all eternity.

    This is probably something straightforward that I am too tired to see.

    Code (CSharp):
    1.     public float speed = 1f;
    2.     private Vector3 target;
    3.     Animator animator;
    4.  
    5.     public void Start()
    6.     {
    7.         target = transform.position;
    8.         animator = GetComponent<Animator>();
    9.     }
    10.  
    11.  
    12.     void Update()
    13.     {
    14.         if (Input.GetMouseButtonDown(0))
    15.         {
    16.             target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    17.             target.z = transform.position.z;
    18.             animator.SetFloat("idle2walk", speed);
    19.  
    20.         }
    21.         transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
    22.    
    23.  
    24.  
    25.  
    26.     }
    27.  
    28. }
    Any help is greatly appreciated.