So I’m trying to create a Dota or LoL type movement(which I am oh so very far from doing) and aquired the click to move script from the wiki. I noticed that the farther I click from the character, the faster he will move to it. I want to keep it the same speed no matter how far the mouse’s position is. How do I do this?
Here’s the script:
// 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;
var marker : GameObject;
private var mousePos : Vector3;
private var worldPos : Vector3;
var User : GameObject;
function Update () {
var controller : CharacterController = GetComponent(CharacterController);
if(Input.GetKeyDown(KeyCode.Mouse1))
{
var playerPlane = new Plane(Vector3.up, transform.position);
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hitdist = 0.0f;
if (playerPlane.Raycast (ray, hitdist)) {
var targetPoint = ray.GetPoint(hitdist);
targetPosition = ray.GetPoint(hitdist);
var targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = targetRotation;
}
if(Input.GetMouseButton(1)) {
mousePos = Input.mousePosition;
mousePos.z = 1;
var cloneMarker = Instantiate(marker,worldPos,Quaternion.identity);
Destroy(cloneMarker, .5f);
}
Debug.DrawLine(User.transform.position, targetPosition, Color.red);
}
transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);
}
@script RequireComponent(CharacterController)
So, what can i do? Thanks for help!