Lerp Not working
I made this script to move my camera up and down smoothly but the lerp is not working. Here is my code:
using UnityEngine; using System.Collections;
public class Move : MonoBehaviour { public Transform startMarker; public Transform endMarker; public float speed = 100.0F; private float startTime; private float journeyLength; public float up = 0.0f; public AudioClip[] audioClip; void PlaySound(int clip)
{ GetComponent().clip = audioClip[clip];
}
// Use this for initialization
void Start () {
startTime = Time.time;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.S)){
transform.position = Vector3.Lerp(transform.position, endMarker.position, speed);
up = 0.0f;
}else{
if (Input.GetKeyDown(KeyCode.W)){
transform.position = Vector3.Lerp(transform.position, startMarker.position,speed);
up = 1.0f;
}
}
if (up == 1.0f){
if (Input.GetKeyDown(KeyCode.E))
{
if(GetComponent<Light>().enabled == false)
{
GetComponent<Light>().enabled = true;
GetComponent<AudioSource>().Play ();
}
else
{
GetComponent<Light>().enabled = false;
GetComponent().Play (); }
}
}
} }
Any help would be greatly appreciated
Answer by mikelortega · Feb 17, 2016 at 08:46 AM
The third argument for Lerp is a float from 0 to 1. You are passing speed, but it should be a variable that increments every update depending on that speed.
Vector3 Lerp(Vector3 a, Vector3 b, float t);
When t = 0 returns a. When t = 1 returns b. When t = 0.5 returns the point midway between a and b.
*Please, format your code with the '101010 Code Sample' button so your question is more readable.
Adding to this answer a common way to lerp in Update()-functions is to use Time.deltaTime (or rather, it should be used). transform.position = Vector3.Lerp(transform.position, start$$anonymous$$arker.position, speed*Time.deltaTime);
Time.deltaTime takes the time passed from the last frame to this frame. This will make your game frame-rate independent. It's really bad to have a frame-rate dependent game.