this is the code, that causes the object to only move when the mouse is down. ‘return’ means that the rest of the Update function is ignored and therefore no objects can be moved.
i’d use
if (Input.GetMouseButtonDown(0)) {
//all the raycast stuff in here
}
to determine the target position
then use
moveVector = Vector3(targetPosition - currentPosition);
transform.position = currentPosition + moveVector.normalized * speed;
.normalized means the vector is only 1 unit long, which means that the object always moves 1 step forward. with the variable speed you can modify the speed of the object, 0 means its standing still, 1 means the speed isn’t modified, the larger the numbers get the faster the object will be, negative numbers will make the object move away from the target.
once moveVector (not normalized) has a length smaller than the speed you should move the object to the target position, otherwise the object will kinda jump around the target position
note that the speed is dependant on the framerate in this solution, you might want to use FixedUpdate instead
to sum it all up:
var rayCastPlane : Transform;
private var wantedPosition : Vector3;
var speed : int = 5;
private var moving : System:Boolean = false;
function FixedUpdate () {
if (Input.GetMouseButtonDown(0)) {
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 100)) {
Debug.DrawLine (Camera.main.transform.position, hit.point, Color.red);
Debug.Log(hit.point);
var wantedPosition = Vector3(hit.point.x, transform.position.y, hit.point.z);
moving = true;
}
}
if(moving) {
var moveVector = Vector3(wantedPosition - transform.position);
if (moveVector.magnitude < speed)
{
transform.position = transform.position + moveVector.normalized * speed;
}
else
{
transform.position = wantedPosition;
moving = false;
}
}
}
there might be some mistakes in the code, i just threw it together very quickly and didn’t test it, but this should get you the basic idea