- Home /
Question by
Chikikhan101 · Jan 11 at 06:46 AM ·
c#movementmovement scriptdelaymove an object
How can I add a delay between moving objects ?
Now all the moving objects are moving like a group to the same target. I need them to move to the same target but with a delay between them.
The first object start moving to the target after X seconds the second object start moving to the target then the third and so on they all should move to the first target but with a delay between the moving start.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveToTarget : MonoBehaviour
{
public AnimationCurve curve = AnimationCurve.EaseInOut(0.0f, 0.0f, 1.0f, 1.0f);
public List<Transform> targets = new List<Transform>();
public List<Transform> objectsToMove = new List<Transform>();
public float duration;
public bool moveToSameTarget = false;
private float t;
private List<Vector3> originPositions = new List<Vector3>();
// Start is called before the first frame update
void Start()
{
t = 0.0f;
curve.postWrapMode = WrapMode.Once;
for (int i = 0; i < objectsToMove.Count; i++)
{
originPositions.Add(objectsToMove[i].transform.position);
}
}
// Update is called once per frame
void Update()
{
t += Time.deltaTime;
float s = t / duration;
for (int i = 0; i < objectsToMove.Count; i++)
{
objectsToMove[i].position = Vector3.Lerp(originPositions[i], targets[0].position, curve.Evaluate(s));
}
}
}
Comment