Click to move speed

I’m having a really weird issue with this script. Depending on the distance of which is clicked the speed of the object will either speed up if the click location is far away or slow down when it is close. I need it to be a constant speed all of the time. I’ve tried changing a bunch of different pieces of close but still can’t figure out the issue at hand. I believe it has something to do with Vector3.Slerp or something with the movement.

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

     

        private var targetPosition:Vector3;

                                

        function Update () {

            if(Input.GetKeyDown(KeyCode.Mouse0))

            {

           

            smooth=1;

            

                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.Slerp (transform.position, targetPosition, Time.deltaTime * smooth);  

           

          

         

         

         }

Your use of Slerp() is strange to me. Typically the start value is not changed each frame, and typically the last parameter is something that counts up. But putting that aside, I recommend you look into using Vector3.RotateTowards(). RotateTowards takes a maxMagnitudeDelta specifying a maximum delta rotation. So your code would be something like:

transform.position = Vector3.RotateTowards (transform.position, targetPosition, 1.5);

Change the last parameter to control the speed of movement.