# Slide Script

using UnityEngine;

public class Slide : MonoBehaviour
{
public float slidespeed = 9f;
public float weight = 5f;

``````public Vector3 currentposition;
public Vector3 slideposition;

void Update()
{
if (Input.GetKey("a"))
{
currentposition = transform.position;
slideposition = transform.forward * slidespeed * Time.deltaTime;
transform.position = Vector3.Lerp(currentposition, slideposition, Time.deltaTime);
}
}
``````

}

It works the first time but stop afterwards the slide position won’t update with transform forward.

The thing is, you are using a Lerp, and it will probably look like the thing you want, but you are actually not lerping anything. So of course I don’t really know how your game is supposed to work, but as I see this script I get the feeling something isn’t right. What I think the plan is, is when you press the a key, a startposition is saved and then you want to lerp towards your target, the slide position.

So what is happening in the script, when a is pressed in this frame, the position is saved, so every frame the currentposition is shifted forwards because you resave it every frame. The slideposition is calculated from the forward vector. Lets pretend it lines up with the worlds z axis, in that case the vector will be (0,0,1). By multiplying this by slidespeed the vector will be (0,0,9). Finally we multiply by deltaTime (something like 0.05). The vector will now be (0, 0, 0.45). When used as a position, like in the lerp, we can round this to Vector3.zero position, so the origin of the world.

Then the lerp. When lerping we need a start, an end and the interpolation value (0 -1). So the currentposition can be anywhere, the slideposition / the endposition is Vector3.zero. That means that if we lerp correctly we will move to the center of the world from the position we are standing. But the problem is also, that this is not the case. The interpolation value entered in the function doesn’t change. Time.deltaTime is roughly the same all the time (0.05 for now). So when we enter this as our value, we actually say: “Move 5% the total distance from my current position to the center of the world.”

Check out this page on the lerp function and see how they change the value over time. Unity - Scripting API: Vector3.Lerp. And if you want to get a position relative to the gameobject, you could use something like transform.TransformPoint(). Unity - Scripting API: Transform.TransformPoint. Ofcourse there is no way you need to do this and it is all up to you. There are many ways to achieve the same.