- Home /
camera Lerp Silly Question
having a C# moment...
My Lerp is Not Lerping over time despite Time.Deltatime
It just SNAPS
My Speed Variable cuts off the Lerp unless it's like 100 (?) in my inspector
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lerp : MonoBehaviour {
public Vector3 startPos;
public Vector3 endPos;
public float speed = .8f;
public float counter = 0;
public float target = 10;
// Update is called once per frame
void Update () {
}
void OnMouseUp()
{
//record where the Camera is
startPos = Camera.main.transform.position;
//make it go forward 1.8 units
endPos = Camera.main.transform.position + Camera.main.transform.forward * 1.8f;
// Move over time
Camera.main.transform.position = Vector3.Lerp(startPos, endPos, (speed*Time.deltaTime));
// set an IEnumerator Coroutine to wait
StartCoroutine(MyWait());
//reset counter when com back from coroutine, so we can use again
counter = 0;
target = 10;
}
IEnumerator MyWait()
{
// wait for seconds
while (counter < target)
{
counter += Time.deltaTime; yield return null;
}
//put the Camera back to where it was
Camera.main.transform.position = Vector3.Lerp(-endPos, startPos, (speed * Time.deltaTime));
}
}
THANKS For Looking
~be
Answer by Bentoon · Feb 22, 2017 at 11:31 PM
@tanoshimi Thank you Tanoshimi,
You are right but I am still having problems;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Lerp : MonoBehaviour {
public Vector3 startPos;
public Vector3 endPos;
public float speed = .8f;
public float counter = 0;
public float target = 10;
public float timeTakenDuringLerp = 1f;
public float distanceToMove = 1.8f;
private float _timeStartedLerping;
//Whether we are currently interpolating or not
private bool _isLerping;
private bool _isLerpingBack;
// Update is called once per frame
void Update () {
}
void OnMouseUp()
{
//record where the Camera is
startPos = Camera.main.transform.position;
//make it go forward 1.8 units
endPos = Camera.main.transform.position + Camera.main.transform.forward * 1.8f;
// Move over time
_isLerping = true;
// Camera.main.transform.position = Vector3.Lerp(startPos, endPos, (speed*Time.deltaTime));
// set an IEnumerator Coroutine to wait
StartCoroutine(MyWait());
//reset counter when com back from coroutine, so we can use again
counter = 0;
target = 10;
}
IEnumerator MyWait()
{
// wait for seconds
while (counter < target)
{
counter += Time.deltaTime; yield return null;
}
//put the Camera back to where it was
// Camera.main.transform.position = Vector3.Lerp(-endPos, startPos, (speed * Time.deltaTime));
_isLerping = false;
_isLerpingBack = true;
}
void FixedUpdate()
{
if (_isLerping)
{
//We want percentage = 0.0 when Time.time = _timeStartedLerping
//and percentage = 1.0 when Time.time = _timeStartedLerping + timeTakenDuringLerp
//In other words, we want to know what percentage of "timeTakenDuringLerp" the value
//"Time.time - _timeStartedLerping" is.
float timeSinceStarted = Time.time - _timeStartedLerping;
float percentageComplete = timeSinceStarted / timeTakenDuringLerp;
//Perform the actual lerping. Notice that the first two parameters will always be the same
//throughout a single lerp-processs (ie. they won't change until we hit the space-bar again
//to start another lerp)
Camera.main.transform.position = Vector3.Lerp(startPos, endPos, percentageComplete);
//When we've completed the lerp, we set _isLerping to false
if (percentageComplete >= 1.0f)
{
_isLerping = false;
}
}
else if (_isLerpingBack)
{
float timeSinceStarted = Time.time - _timeStartedLerping;
float percentageComplete = timeSinceStarted / timeTakenDuringLerp;
Camera.main.transform.position = Vector3.Lerp(endPos, startPos, percentageComplete);
//When we've completed the lerp, we set _isLerping to false
if (percentageComplete >= 1.0f)
{
_isLerpingBack = false;
}
}
}
}
Any ideas?
Thanks
~be
You're never setting _timeStartedLerping, so percentageComplete is just set to Time.time, which is considerably greater than 1 after the first second of runtime. Set _timeStartedLerping to Time.time when _isLerping or _isLerpingBack is set to make your script work.
hello @$$anonymous$$arshallN
You dont mean something like :
if (_isLerpingBack)
{
_timeStartedLerping = Time.time;
float timeSinceStarted = Time.time - _timeStartedLerping;
float percentageComplete = timeSinceStarted / timeTakenDuringLerp;
Camera.main.transform.position = Vector3.Lerp(endPos, startPos, percentageComplete);
//When we've completed the lerp, we set _isLerping to false
if (percentageComplete >= 1.0f)
{
_isLerpingBack = false;
}
}
Thanks!
~be
No, no, actually nothing like that, haha - that would make timeSinceStarted equal to 0 at all times, since _timeStartedLerping would be set to Time.time every frame. What you want to do is set _timeStartedLerping whenever you toggle_isLerping or _isLerpingBack.
So, in your On$$anonymous$$ouseUp()
function, set _timeStartedLerping = Time.time
.
Then, your coroutine should look like
IEnumerator $$anonymous$$yWait()
{
// wait for seconds
while (counter < target)
{
counter += Time.deltaTime; yield return null;
}
//put the Camera back to where it was
// Camera.main.transform.position = Vector3.Lerp(-endPos, startPos, (speed * Time.deltaTime));
_isLerping = false;
_isLerpingBack = true;
_timeStartedLerping = Time.time; // THIS IS THE LINE I ADDED
}
Answer by hellojbb1234 · Feb 22, 2017 at 08:31 AM
Try increasing your speed variable I don't know exactly but I think if the Lerp is to low it will do what your describing, aka just snap into a position immediately.
Answer by tanoshimi · Feb 21, 2017 at 11:07 PM
You're using Lerp incorrectly because you're never incrementing the third parameter. This is a common problem and has bedn explained well before, e.g. at http://www.blueraja.com/blog/404/how-to-use-unity-3ds-linear-interpolation-vector3-lerp-correctly
Your answer
Follow this Question
Related Questions
Camera not slowly moving to object 0 Answers
How to smooth my camera (Vector3.Lerp) 0 Answers
Use Lerp Position and Slerp Rotation together 0 Answers
How to lerp a child towards parent position while parent is moving 1 Answer
Pan-Lerp a camera 1 Answer