How can I make a Lerping object pause at each point? [SOLVED]
I'm relatively new to programming, and am having a bit of trouble with something. I've successfully gotten an object to move back and forth between two points, through Lerping. However, I want it to pause at each point, for about a second, rather than just instantly moving back. Here's a code sample to show how I'm doing it:
public class ClassName : MonoBehaviour
{
[SerializeField] Transform point1;
[SerializeField] Transform point2;
private float moveSpeed = 2.0f;
private float length = 1.0f;
private void Update()
{
Move();
}
private void Move()
{
//Create a float that goes back and forth between 0 and length
float timer = Mathf.PingPong(moveSpeed * Time.time, length);
transform.position = Vector3.Lerp(point1.position, point2.position, timer);
}
}
The code above makes the object move back and forth between the two points flawlessly, but without pausing. I tried doing something like this, to make it pause:
public class ClassName : MonoBehaviour
{
[SerializeField] Transform point1;
[SerializeField] Transform point2;
private float moveSpeed = 2.0f;
private float length = 1.0f;
private float waitTime = 1.0f;
private bool waiting = false;
private void Update()
{
Wait();
Move();
}
private void Move()
{
if (waiting) { return; }
//Create a float that goes back and forth between 0 and length
float timer = Mathf.PingPong(moveSpeed * Time.time, length);
transform.position = Vector3.Lerp(point1.position, point2.position, timer);
if (timer < 0.001f || timer > 0.999f)
{
waiting = true;
}
}
private void Wait()
{
if (!waiting) { return; }
waitTime -= Time.deltaTime;
if (waitTime <= 0.0f)
{
waitTime = 1.0f;
waiting = false;
}
}
}
However, this ended up giving me a result where the object moves to the second point and waits... but then just teleports back to the first point, rather than moving back.
As I said at the start, I want the object to move back and forth between the two points, like that first block of code I posted accomplishes, but I ALSO want it to pause at each point when it reaches them. Can anyone help me with this?
Answer by Hellium · Jun 29, 2019 at 03:28 PM
using UnityEngine;
public class ClassName : MonoBehaviour
{
[SerializeField] Transform point1;
[SerializeField] Transform point2;
private float moveSpeed = 2.0f;
private float waitTime = 1.0f;
private void Update()
{
Move();
}
private void Move()
{
//Create a float that goes back and forth between `-waitTime * 0.5f` and `1 + waitTime * 0.5f`
float timer = Mathf.PingPong(moveSpeed * Time.time, 1 + waitTime) - waitTime * 0.5f;
// Clamp the value between 0 and 1
// so when timer ∈ [-waitTime * 0.5f , 1] or timer ∈ [1, 1 + waitTime * 0.5f ],
// the object will be stopped
timer = Mathf.Clamp( timer, 0, 1 );
transform.position = Vector3.Lerp(point1.position, point2.position, timer);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612204047im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to change animator states when a key is held through a C# script for a sprite? 0 Answers
Activating multiple images under one canvas via multiple objects 0 Answers
Error, Raycast2D call function in self script on duplicate object. 0 Answers
Ground check BoxCast is detecting when player hits bottom of platform. UNITY 2D C# 0 Answers