I know this topic has been plastered all over, but it is unbaringly horrid to try and traverse through many different peoples methods to find what I am looking for.
All I am after is something the moves from A to B within a time frame thats it. no special features, no smoothing, just a basic, in and out within a time.
My current code, which obviously doesnt work (works okay. but doesnt have a ‘smooth’ movement, it just goes from a to b in an instant, how can I make it so the ‘speed’ actually is taken into accordance?
public void openBottom()
{
bottomDraw.transform.localPosition = new Vector3(bTempX, bTempY, Mathf.Lerp(closePos, openPos, speed));
bottomOpen = true;
}
public void closeBottom()
{
bottomDraw.transform.localPosition = new Vector3(bTempX, bTempY, Mathf.Lerp(openPos, closePos, speed));
bottomOpen = false;
}
If you want something that moves linearly between A and B over a set period of time, then you need to keep track of time itself;
//Lerp start
public Vector3 start;
//Lerp end
public Vector3 end;
//Total time of the lerp in seconds
public float seconds = 2f;
private float startTime = 0f;
private bool runFunction = false;
void Update ()
{
//Start the coroutine once when the space key is pressed
if (Input.GetKeyDown (KeyCode.Space))
StartCoroutine (LerpPosition (start, end, seconds));
//Alternatively, run the function and set the current time when the mouse is clicked
if (Input.GetKeyDown (KeyCode.Mouse0) && !runFunction)
{
startTime = Time.time;
runFunction = true;
}
if (runFunction)
{
LerpPosition2 (start, end, seconds);
}
}
private bool isRunning = false;
//I'm using an enumerator to make things a bit easier, but there's also an example with a normal function below
IEnumerator LerpPosition (Vector3 a, Vector3 b, float s)
{
if (isRunning)
yield break;
isRunning = true;
float startTime = 0f;
float time = startTime;
while (time < s)
{
transform.position = Vector3.Lerp (a, b, (time - startTime) / s);
time += Time.deltaTime;
yield return null;
}
isRunning = false;
}
void LerpPosition2 (Vector3 a, Vector3 b, float s)
{
float time = (Time.time - startTime) / s;
transform.position = Vector3.Lerp (a, b, time);
if (time >= 1f)
runFunction = false;
}