How do i make the player to be "Idle" when reaching the mouse position ?

And what is this bug thing in this line ?

idleState = Animator.StringToHash("Idle");

This is not my script not all the code is my script.

In my project in Window > Animator i created two states called them: Walk and Idle for the Walk i’m using the HumanoidWalk animation and for the Idle the HumanoidIdle.

Then i added to the script that when i click the mouse the player walk animation will start. Then when the player is getting to the mouse position he stop moving but the Walk animation is keep animated so whet i see is the player walking on place.

What i want to do is when the player reached the target position make him “Idle”.

But can’t find where and how to do it.

using UnityEngine;
using System.Collections;

public class PlayerMovements : MonoBehaviour {

    private Transform myTransform;              // this transform
    private Vector3 destinationPosition;        // The destination Point
    private float destinationDistance;          // The distance between myTransform and destinationPosition
    public float Speed;                         // Speed at which the character moves
    public float Direction;                    // The Speed the character will move
    public float moveAnimSpeed;                // Float trigger for Float created in Mecanim (Use this to trigger transitions.
    public float animSpeed = 1.5f;            // Animation Speed
    public Animator anim;                     // Animator to Anim converter
    public int idleState = Animator.StringToHash("Base Layer.Idle"); // String to Hash conversion for Mecanim "Base Layer"
    public int runState = Animator.StringToHash("Base Layer.Run");  // String to Hash for Mecanim "Base Layer Run"
    private AnimatorStateInfo currentBaseState;         // a reference to the current state of the animator, used for base layer
    private Collider col;




    void Start()
    {

        Physics.gravity = new Vector3(0, -200f, 0); // used In conjunction with RigidBody for better Gravity (Freeze Rotation X,Y,Z), set mass and use Gravity)
        anim = GetComponent<Animator>();
        idleState = Animator.StringToHash("Idle"); // Duplicate added due to Bug
        runState = Animator.StringToHash("Run");
        myTransform = transform;                            // sets myTransform to this GameObject.transform
        destinationPosition = myTransform.position;
        // prevents myTransform reset
    }

    void FixedUpdate()
    {

        // keep track of the distance between this gameObject and destinationPosition      

        currentBaseState = anim.GetCurrentAnimatorStateInfo(0);

        destinationDistance = Vector3.Distance(destinationPosition, myTransform.position);

        // Set's speed in reference to distance

        if (destinationDistance < .5f)
        {
            Speed = 2;
        }
        else if (destinationDistance > .5f)
        {
            Speed = 2;

            //Below sends Floats to Mecanim, Raycast set's speed to X until destination is reached animation is played until speed drops
        }

        if (Speed > .5f)
        {
            anim.SetFloat("moveAnimSpeed", 0.1f);
        }

        else if (Speed < .5f)
        {
            anim.SetFloat("moveAnimSpeed", 0.1f);
        } //


        // Moves the Player if the Left Mouse Button was clicked


        if (Input.GetMouseButtonDown(0) && GUIUtility.hotControl == 0)
        {
            anim.CrossFade("Walk", 0);
            Plane playerPlane = new Plane(Vector3.up, myTransform.position);
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            float hitdist = 0.0f;

            if (playerPlane.Raycast(ray, out hitdist))
            {
                Vector3 targetPoint = ray.GetPoint(hitdist);
                destinationPosition = ray.GetPoint(hitdist);
                Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
                myTransform.rotation = targetRotation;           
                anim.CrossFade("Idle", 0);
            }

        }
        
   
       
 
        // Moves the player if the mouse button is hold down
        else 
        if (Input.GetMouseButton(0) && GUIUtility.hotControl == 0) {
            anim.CrossFade("Walk", 0);
            Plane playerPlane = new Plane(Vector3.up, myTransform.position);
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            float hitdist = 0.0f;

            if (playerPlane.Raycast(ray, out hitdist))
            {
                Vector3 targetPoint = ray.GetPoint(hitdist);
                destinationPosition = ray.GetPoint(hitdist);
                Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
                myTransform.rotation = targetRotation;
            }
            //  myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, moveSpeed * Time.deltaTime);
            
        }

        // To prevent code from running if not needed
        if (destinationDistance > .5f)
        {
            myTransform.position = Vector3.MoveTowards(myTransform.position, destinationPosition, Speed * Time.deltaTime);
        }
    }
}

In your code for moving the player if the mouse is held down, modify your if to include a check for the mouse position relative to the player position, if they are equal set the player to idle.

Unity - Scripting API: Input.mousePosition ← this is the method that returns the coordinates of the mouse. So you should check the players transform.position relative to the mouses :slight_smile:

If you need more help with how to code that then let me know!