- Home /
Keeping the character's speed the same
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!
Comment
Best Answer
Answer by gabs · Feb 08, 2012 at 12:11 AM
Remove the easing. Instead doing "transform.position = Vector3.Lerp (transform.position, targetPosition, Time.deltaTime * smooth);", do like:
float constantSpeed = 1f;
transform.position = transform.position + (targetPosition - transform.position).normalized * constantSpeed * Time.deltaTime;
Your answer
Follow this Question
Related Questions
How to move object along Spline at constant speed? 0 Answers
Bullet not moving from script 3 Answers
Change int value for 30 sec... 1 Answer