- Home /
I'm using TRANSLATE but want to use LERP. C#
Hi everyone,
I'm working on a project that's going to require a lot of fluid motion. There are going to be documents on a table that will need to move toward the camera when clicked. As of right now, I only have one problem: using a lerp instead of a translate to move my camera.
Here is what is working now:
void TransitionCamera(){
Scene.transform.Translate (Vector3.down * 100);
selectionComplete = true;
}
void TransitionCameraBack(){
Scene.transform.Translate (Vector3.up * 100);
displayScore = true;
}
Works fine to jump the Scene (including the main camera and the scene background) down and back up again. But I don't want the Scene to jump, I want it to move smoothly over a few seconds. This will make it seem as if objects in the scene are floating upward, and then floating back into the scene when TransitionCameraBack is called.
Can anyone help me? I've tried implementing the Unity documentation on Vector3.Lerp. It didn't help to smooth the transition, I got some errors, I lost a lot of sweat and time, and then I reverted to this original script out of fear.
Thank you a billion for taking the time to read!
People may find it easier to help if you show the broken code. While we could write a snippet, you're more likely to find out exactly what you were doing wrong that way. It would be particularly useful if you say specifically what errors you got (from what you've said its hard to be sure if you mean compiler errors or runtime errors, for example)
A common pitfall is not setting the t value correctly- it should be between 0 and 1, and vary over time. Lerp also needs to be called each time you want to update the position (so probably every frame the object is moving). The most common way to do that is with a coroutine, which can cause more problems for beginners.
Well, what I did was mixed this simple above code with some kind of bastardization of the Vector3.Lerp example code. I think this is quite similar to what I was using:
public GameObject Scene;
public GameObject SceneTarget;
public Transform start$$anonymous$$arker;
public Transform end$$anonymous$$arker;
public float speed = 1.0F;
private float startTime;
private float journeyLength;
public Transform target;
bool moveCameraUp;
bool moveCameraDown;
void TransitionCamera(){
startTime = Time.time;
start$$anonymous$$arker = Scene.transform;
end$$anonymous$$arker = SceneTarget.transform;
journeyLength = Vector3.Distance(start$$anonymous$$arker.position, end$$anonymous$$arker.position);
moveCameraDown = true;
moveCameraUp = false;
selectionComplete = true;
}
void TransitionCameraBack(){
startTime = Time.time;
journeyLength = Vector3.Distance(end$$anonymous$$arker.position, start$$anonymous$$arker.position);
moveCameraDown = false;
moveCameraUp = true;
displayScore = true;
}
void Update() {
if(moveCameraUp){
float distCovered = (Time.time - startTime) * speed;
float fracJourney = distCovered / journeyLength;
transform.position = Vector3.Lerp(start$$anonymous$$arker.position, end$$anonymous$$arker.position, fracJourney);
}
if(moveCameraDown){
float distCovered = (Time.time - startTime) * speed;
float fracJourney = distCovered / journeyLength;
transform.position = Vector3.Lerp(end$$anonymous$$arker.position, start$$anonymous$$arker.position, fracJourney);
}
}
Answer by whydoidoit · Jun 21, 2013 at 07:42 AM
You could try this:
IEnumerator TransitionCamera()
{
var t = 0f;
var start = Scene.transform.position;
while(t < 1)
{
t += Time.deltaTime;
Scene.transform.position = Vector3.Lerp(start, start + Vector3.down * 100, t);
yield return null;
}
selectionComplete = true;
}
Then call it using:
StartCoroutine(TransitionCamera());
I'm unfamiliar with the I() calls like IEnumerator. Is that a class? Does it go outside of Start() like TransitionCamera would? All I know about it is that it is a "coroutine", but I don't know what that means.
I'll plug it in and see if it works. Thank you again for your help!
It's what is known as an interface (the specification of some functions that must be implemented, it basically allows an object to provide multiple common functions). In the case of Unity it uses IEnumerator to run coroutines - routines that run across multiple frames of the game.
The yield return null;
causes the routine to pause until the next frame.
Answer by Bunny83 · Jun 21, 2013 at 07:50 AM
I don't use it that much myself, but i should mention it:
which is a free library and offers tons of lerping and tweening functions and is very easy to use. In most cases i implement simple tweens like whydoidoit showed in his answer because you have more control ;)
Your answer
Follow this Question
Related Questions
Vector3.Lerp moves in the wrong direction 1 Answer
Slide object between two moving points c# 1 Answer
Smoothly Rotate Character Y to Camera Y 2 Answers
MouseWheel Lerp Smoothing Problem 1 Answer
First Person Character, Mecanim. 1 Answer