Move to clicked point.

Hello guys i want to ask a question. Is there any script to click and move to a point?

I found a script ClickToMove in unify (unity wiki) but i have a problem… when i press on a far distance my character goes to that point very fast but when i press to a short distance my character goes slow. I want to be the same speed at all distances. And if its easy how can i make the animation plays when the character is between the starter point and the clicked point? Like walk animation and when he goes to that clicked point to play the animation idle.

Thank you for the help…
Here is the script which i found on unity.If there is a way so you can edit it to keep the speed same on all distances i will be happy :slight_smile:

// Click To Move script
// Moves the object towards the mouse position on left mouse click

var smooth:int; // Determines how quickly object moves towards position

private var targetPosition:Vector3;

function Update () {
    if(Input.GetKeyDown(KeyCode.Mouse0))
    {
        var playerPlane = new Plane(Vector3.up, transform.position);
        var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
        var hitdist = 0.0;
        
        if (playerPlane.Raycast (ray, hitdist)) {
            var targetPoint = ray.GetPoint(hitdist);
            targetPosition = ray.GetPoint(hitdist);
            var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
            transform.rotation = targetRotation;
        }
    }
    
    transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);
}

As @kevork said, Lerp isn’t the solution in your case. If you want a constant velocity, use MoveTowards instead: MoveTowards(pointA, pointB, delta) returns a point in the line pointA-pointB distant delta units from pointA - and clamped to pointB, thus it never goes beyond the destination point.

Just replace the Lerp line with this:

  transform.position = Vector3.MoveTowards(transform.position, targetPosition, Time.deltaTime * speed);

where speed is in meters (or units) per second.

hi
my player touchtarget complete move but target to stop not a stop continues move in this direction

my current code
transform.position = Vector3.MoveTowards(transform.position, targetPosition, Time.deltaTime * speed);