'How to start the lerp over again automatic each time the value reached to the end/start?
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class DistanceCheck : MonoBehaviour
{
float lerpDuration = 3;
public GameObject descriptionTextImage;
public TextMeshProUGUI text;
private Animator anim;
private bool slowDownOnExit = false;
private float timeElapsed = 0;
private float startValue = 1;
private float endValue = 0;
private float valueToLerp = 0;
// Start is called before the first frame update
void Start()
{
anim = transform.GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (slowDownOnExit)
{
if (timeElapsed < lerpDuration)
{
valueToLerp = Mathf.Lerp(startValue, endValue, timeElapsed / lerpDuration);
anim.SetFloat("Forward", valueToLerp);
timeElapsed += Time.deltaTime;
}
anim.SetFloat("Forward", valueToLerp);
descriptionTextImage.SetActive(true);
text.text = "I can't move that far by foot. I need to find some transportation to move any further.";
}
else
{
text.text = "";
descriptionTextImage.SetActive(false);
}
}
private void OnTriggerExit(Collider other)
{
if(other.name == "CrashLandedShipUpDown")
{
slowDownOnExit = true;
}
}
}
Now the startValue is 1 and the endValue is 0 i want that when the lerp is finish and decreasing slowing down the speed by changing the float value to 0 that it will start increasing the float back to 1 then when reaching to 1 to slow down again and so on nonstop.
also how can i display the text when the endValue is at 0 no the text show once it's start slow down and i want to display the text when it's reaching to 0 stopped.
Solution 1:[1]
You could add one more varible currentValue and lerp that like following
private float startValue = 1;
private float endValue = 0;
private float targetValue;
private float currentValue;
private void Awake() {
targetValue = startValue;
}
private void Update() {
currentValue = Mathf.Lerp(currentValue, targetValue, lerpTime);
if (currentValue == startValue) {
targetValue = endValue;
}
if (currentValue == endValue) {
targetValue = startValue;
}
}
Here we check for currentValue and update the target accordingly so it will continue to ping pong from 0 to 1
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Digvijaysinh Gohil |
