- Home /
Problem with click-to-move script
I have this kind-of standar scrip for moving to a clicked position:
// The target we are following var target : Transform; // The distance in the x-z plane to the target var distance = 10.0; // the height we want the camera to be above the target var height = 5.0; // How much we var heightDamping = 2.0; var rotationDamping = 3.0;
// Place the script in the Camera-Control group in the component menu @script AddComponentMenu("Camera-Control/Smooth Follow")
function LateUpdate () { // Early out if we don't have a target if (!target) return; // Calculate the current rotation angles var wantedRotationAngle = target.eulerAngles.y; var wantedHeight = target.position.y + height; var currentRotationAngle = transform.eulerAngles.y; var currentHeight = transform.position.y; // Damp the rotation around the y-axis currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
// Damp the height currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
// Convert the angle into a rotation var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0); // Set the position of the camera on the x-z plane to: // distance meters behind the target transform.position = target.position; transform.position -= currentRotation Vector3.forward distance;
// Set the height of the camera transform.position.y = currentHeight; // Always look at the target transform.LookAt (target); }
The problem is that, when I click to far from the object, it moves toward the click at an insane speed, evading all the colliders in the way ¿Any idea of how to solve it? Thanks!!!!
Your answer
![](https://koobas.hobune.stream/wayback/20220613081048im_/https://answers.unity.com/themes/thub/images/avi.jpg)