- Home /
Question by
smirlianos · Mar 13, 2013 at 06:54 PM ·
javascriptraycastinputpositionmouse
Move player with mouse help
I have this script that moves the object on a plane on the mouse position.
// Click To Move script
// Moves the object towards the mouse position on right mouse click
var smooth : float; // Determines how quickly object moves towards position
var selected : boolean;
private var targetPosition : Vector3;
function Update () {
if(Input.GetKeyDown(KeyCode.Mouse1) && selected)
{
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);
}
function OnMouseStay() {
if(Input.GetKeyDown(KeyCode.Mouse0))
{
selected = true;
}
}
but there are some bugs. firstly the object falls through the ground slowly, and it moves in a long distance very fast and on short distances very slow. How can I set the speed to be the same always??
Comment
Try Vector3.$$anonymous$$oveTowards() ins$$anonymous$$d of Vector3.Lerp() to solve your speed issues.