void Update()
{
if (targetPos != null && (isMoving))
{
this.transform.LookAt(targetPos);
this.transform.position = Vector3.Lerp(transform.position, targetPos, Time.deltaTime * speed);
}
}
Im bulding bubbles game, the bubble is projected but slowing down near the target, is there any way go around it?
The point of “lerping yourself” towards the target is to get that fast-then-slow movement. Main use is there’s no limit on speed. For example, if you teleport, a camera using normal movement will slowly scroll to catch up with you. A lerping camera will zoom right over.
If you don’t want fast then slow movement, use one of the other methods of moving. A general way is using MoveTowards
with a speed you control. The syntax is the same as lerp, but the 3rd input is more the speed in meters/frame::
float spd = 5;
trans.pos = Vector3.MoveTowards( trans.pos, targetPos, spd*Time.deltaTime);
// 5 meters/sec
That moves at a constant rate of spd
meters per second. To change the rate, change speed:
// slow down to 2 meters/sec:
spd -= 2*Time.deltaTime;
if(spd<2) spd=2;
// quickly get faster, up to 20 M/sec:
spd += 10*Time.deltaTime;
if(spd>20) spd=20;
I believe that the final parameter in Lerp is supposed to be a value between 0 and 1, where 0 is 0% to the target (at transform.position) and 1 is 100% to the target (at targetPos). The reason it would slow down is because Time.deltaTime * speed will probably never move towards 1 and give you what you are looking for.
Here is an example of how you could modify this to work; you will need to figure out how much time you want the move to take, but you can probably derive this from the speed with speed = distance / time => time = speed / distance.
float timeToLerp = 2; //lerp for two seconds.
Vector3 startingPosition;
float timeLerped = 0.0f;
void Start()
{
startingPosition = transform.position;
}
void Update()
{
timeLerped += Time.deltaTime;
transform.position = Vector3.Lerp(startingPosition, targetPos, timeLerped / timeToLerp);
}
Second, as you’ll see in the code example, you need to save the starting position in a separate variable. If you are updating “transform.position” at the same time that you are using it as the starting position in the Lerp, you’ll get some weird results.
Instead of using your current position with transform.position, try storing your current position that doesn’t change as a start position and use that fixed position for the lerp. Because by using transform.position you continuously update your startposition.