Lerping another gameobject 1 unit forward then stop and repeat.
Hi Unity Forum, I'm pretty new to C# and are thankful for all the help I can get.
I have 1x1unit conveying belts that I can move and rotate around. what I want is for them to move another game object 1 unit smoothly forward when stop for 1 sec and so on. I got this far, but it only "snaps the object" 1 unit per frame forward. My guess is that Lerp does not work inside OnTriggers. (I Have a collider on the belt, and a collider with a rigged body on the "Item" I want to move.) The "start and End Marker" Transform has empty game objects on the positions I want them to move in between.
public Transform startMarker;
public Transform endMarker;
// Time when the movement started.
private float startTime;
// Total distance between the markers.
private float journeyLength;
void Start()
{
// Keep a note of the time the movement started.
startTime = Time.time;
// Calculate the journey length.
journeyLength = Vector3.Distance(startMarker.position, endMarker.position);
}
private void OnTriggerStay(Collider other)
{
// Distance moved equals elapsed time times speed..
float distCovered = (Time.time - startTime) * speed;
// Fraction of journey completed equals current distance divided by total distance.
float fractionOfJourney = distCovered / journeyLength;
// Set our position as a fraction of the distance between the markers.
other.transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fractionOfJourney);
}
Your answer
Follow this Question
Related Questions
How do you detect a mouse button click on a Game Object? C# 2 Answers
Destroying GameObject on collision Problems (Begginer) 1 Answer
OnCollisionEnter2D is called after object was shifted 1 Answer
orbiting mouse controlled camera goes through terrain 0 Answers
Deleting an object from a list of objects from another script. 1 Answer