using UnityEngine;
using System.Collections;
public class MainMenuIntro : MonoBehaviour {
public Transform startPos;
public Transform endPos;
private float minIntensity = 0.6f;
private float maxIntensity = 0.8f;
private float minRange = 30f;
private float maxRange = 30f;
private float minSpotAngle = 55f;
private float maxSpotAngle = 60f;
void Start () {
transform.position = new Vector3(startPos.position.x, startPos.position.y, startPos.position.z);
}
void Update () {
transform.position = Vector3.Lerp(startPos.position, endPos.position, Time.deltaTime);
light.intensity = Random.Range(minIntensity,maxIntensity);
light.range = Random.Range(minRange,maxRange);
light.spotAngle = Random.Range(minSpotAngle,maxSpotAngle);
}
}
3 Answers
3
you need are using lerp incorrectly to get your desired effect.
just change:
transform.position = Vector3.Lerp(startPos.position, endPos.position, Time.deltaTime);
to:
transform.position = Vector3.Lerp(transform.position, endPos.position, Time.deltaTime);
what was happening with your settup is that each time it returns the same value for the vector3, what you need to do is pass the new vector3 back into the lerp to get the next value.
Switch to Vector3.MoveTowards( transform.position, endPos.position, Time.deltaTime ).
Using Vector3.Lerp will never get you to that endPos.position exactly, but it will get very close to the endPos.position. To explain better. I will use Mathf.Lerp (the following is not code, it is a sequence of event):
start = 0;
end = 1;
step = 0.1;
start = Mathf.Lerp( start, end, step );
//start becomes 0.1
start = Mathf.Lerp( start, end, step );
//start becomes 0.19
start = Mathf.Lerp( start, end, step );
//start becomes 0.271
start = Mathf.Lerp( start, end, step );
//start becomes 0.3439
What happen here is that the start will become very close to end, but it will never become exactly end. Lerp stands for linear interpolation, something like this ( end-start )*step + start. If you put in 0.5, you will get the middle of both values. If you put in 1, you will go to the end straight away, not matter far it is.
If someone is also struggling with the Lerp, and if it’s not working. Then I would suggest you my article where I have described:
- What is Lerp In Unity?
- How Does Lerp Work In Unity?
- Right Way To Use Lerp, Multiple Ways.
- Bonus Tips
If you are a beginner or even an intermediate, then I would suggest you check out my article, Click Here