Hi, I have a problem with this code
this code is for move an object to another Game object and my problem is that in different distance, speed get changed
for example if destination be close my object move to it slowly but if destination be far object move to it so fast

``````// Follows the target position like with a spring

var target : Transform;
var smooth = 0.3;
function Update () {
transform.position = Vector3.Lerp (
transform.position, target.position,
Time.deltaTime * smooth);
}
``````

can someone help me please ?

What exactly is the problem? I assume the problem is that it barely moves at all. Is that correct?

The reason for this would be because it is only lerping a short distance towards the target. Assuming your frame rate is 60 fps. So the lerp amount would be (0.016666 * 0.3) or 0.005. So it is only moving about 0.5% towards your target each frame. So you need to adjust your lerp amount to move a greater distance.

thanks but my problem is speed of move!!!

look at this code (this code move an object in plane by clicking )

``````var smooth:int; // Determines how quickly object moves towards position

private var targetPosition:Vector3;

function Update () {
if(Input.GetKeyDown(KeyCode.Mouse0))
{
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 * 0.2);
}
``````

now my problem is when I click on near of distance of game object, game object move slowly and good to destination
but when I click on far distance game object reaches to destination too fast and speed get faster
speed is depend on distance

how can I solve that problem ?

try Smooth * time.deltatime

Example code -

transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.LookRotation(target.transform.position - transform.position), 5 * Time.deltaTime);
transform.position += transform.forward * 7.5F * Time.deltaTime;

thanks but Iβve use Smooth before and I still have speed problem

like this ?

``````var smooth:int; // Determines how quickly object moves towards position

private var targetPosition:Vector3;

function Update () {
if(Input.GetKeyDown(KeyCode.Mouse0))
{
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);
}
``````

Solved, thanks a lot

How did you solve it? I have the same problem, the object slows down when itβs getting closer to the target.

Sorry to bump this, but I kinda needs to solve this. So how did you solve it?