- Home /
Lerping between Camera Paths
Helloooo
I have a camera with different iTween Paths on it
I am simply jumping back and forth by activating and deactivating them...
It's crude, I know, but it's actually the best solution so far...
But I want the Camara to LERP between the two paths instead of just Jumping immediately.
Here is the C# code so far:
Thanks for looking
using UnityEngine;
using System.Collections;
public class switchITween : MonoBehaviour {
Vector3 firstCam;
Vector3 secondCam;
void OnMouseUp() {
// these are a bunch of Path scripts on the main camera that I am activating and deactivating
FlythroughCameraController1 a;
FlythroughCameraControllerA b;
a=Camera.mainCamera.GetComponent<FlythroughCameraController1>();
b=Camera.mainCamera.GetComponent<FlythroughCameraControllerA>();
// float i = 0.0f;
// float rate = 1.0/Time;
// These Activate and deactivate the path scripts, but I want that camera not just to JUMP to the newly active payh but Lerp there
if (a.enabled == true) {
Debug.Log ("B");
a.enabled = false;
b.enabled = true;
// while (i>1.0) {
// i += Time.deltaTime * rate;
// Camera.main.transform.position = Vector3.Lerp(firstCam, secondCam, i);
//
// }
}
//
//
//
else if (b.enabled == true) {
Debug.Log ("A");
b.enabled = false;
a.enabled = true;
// while (i>1.0){
// i+= Time.deltaTime * rate;
// Camera.main.transform.position = Vector3.Lerp(secondCam, firstCam, i);
//
// }
}
//
}
}
Answer by Aeleck · May 15, 2015 at 07:04 AM
Vector3.Lerp function's third parameter is a number between 0 and 1. It looks like you dont execute unless i is already above 1 therefore the Lerp function doesn't have a chance to interpolate between your two points. Also, OnMouseUp is called once and Lerping is usually best done over several frames otherwise your camera will just go part ways to the second point and stop. So what I would suggest is using
while(i < 1.0f) {
i+= Time.deltaTime * rate;
Camera.main.transform.position = Vector3.Lerp(secondCam, firstCam, i);
}
Hope this helps! Good luck :)
Thanks Aeleck,
Now I get an error on this line:
float rate = 1.0/Time;
Time is a class and cannot be mathematical used. You could actually just set rate to however many seconds you want the lerp to last since Time.deltaTime
will make that happen and adding it to i
with 1 / rate
will keep the lerp constant (not exponential). If you want the lerp to last two seconds then try setting rate
to 2
and then have the line
i+= Time.deltaTime * rate;
become
i += Time.deltaTime * 1/rate;
The reason we need to take 1/rate is because of the lerp needing to be between 0 and 1 and percentages and all that fun stuff. Also, if you put this in the update method you can get rid of the While
loop, you dont want it all happening at once. Just check if the i is less than 1.
Aeleck
Thanks for your help.
This plays, but doesn't Lerp,
So I put the path switching after the Lerp with a wait for seconds in there
Still has trouble,
.. I am more concerned with is the fact that the mouse Up / Down are only working 15% of the time on $$anonymous$$obile. Crazy
using UnityEngine;
using System.Collections;
public class switchITween4 : $$anonymous$$onoBehaviour {
//variable to hold the point where two paths intersect to Lerp to
public Vector3 newCamPos;
float i = 0.0f;
public float rate = 2f;
void On$$anonymous$$ouseDown() {
// these are a bunch of Path scripts on the main camera that I am activating and deactivating
Debug.Log ("$$anonymous$$OUSE");
Vector3 CamPos = Camera.main.transform.position;
FlythroughCameraController1 a;
FlythroughCameraControllerA b;
a=Camera.mainCamera.GetComponent<FlythroughCameraController1>();
b=Camera.mainCamera.GetComponent<FlythroughCameraControllerA>();
// These Activate and deactivate the path scripts, but I want that camera not just to JU$$anonymous$$P to the newly active payh but Lerp there
if (a.enabled == true) {
Debug.Log ("B");
while(i < 1.0f) {
i+= Time.deltaTime * 1/rate;
Camera.main.transform.position = Vector3.Lerp(CamPos, newCamPos, i);
}
StartCoroutine (myWait ());
a.enabled = false;
b.enabled = true;
}
else if (b.enabled == true) {
Debug.Log ("A");
while(i < 1.0f) {
i+= Time.deltaTime * 1/rate;
Camera.main.transform.position = Vector3.Lerp(CamPos, newCamPos, i);
}
StartCoroutine (myWait ());
b.enabled = false;
a.enabled = true;
}
}
IEnumerator myWait()
{
Debug.Log ("myWait");
yield return new WaitForSeconds (2.0f);
}
]
// void OnApplicationQuit ()
// {
// FlythroughCameraController1 a;
// FlythroughCameraControllerA b;
// a=Camera.mainCamera.GetComponent<FlythroughCameraController1>();
// b=Camera.mainCamera.GetComponent<FlythroughCameraControllerA>();
// a.enabled = true;
// }
You've got to move the lerping into the Update method and get rid of the While statements. Try this:
using UnityEngine;
using System.Collections;
public class switchITween4 : $$anonymous$$onoBehaviour {
//variable to hold the point where two paths intersect to Lerp to
public Vector3 newCamPos;
float i = 0.0f;
public float rate = 2f;
private bool lerping = false;
void Update() {
if(lerping) {
// These Activate and deactivate the path scripts, but I want that camera not just to JU$$anonymous$$P to the newly active payh but Lerp there
if (a.enabled == true) {
Debug.Log ("B");
if(i < 1.0f) {
i+= Time.deltaTime * 1/rate;
Camera.main.transform.position = Vector3.Lerp(CamPos, newCamPos, i);
} else if(i >= 1.0f) {
lerping = false;
i = 0.0f;
a.enabled = false;
b.enabled = true;
}
StartCoroutine (myWait ());
} else if (b.enabled == true) {
Debug.Log ("A");
if(i < 1.0f) {
i+= Time.deltaTime * 1/rate;
Camera.main.transform.position = Vector3.Lerp(CamPos, newCamPos, i);
} else if(i >= 1.0f) {
lerping = false;
i = 0.0f;
b.enabled = false;
a.enabled = true;
}
StartCoroutine (myWait ());
}
}
}
}
void On$$anonymous$$ouseDown() {
// these are a bunch of Path scripts on the main camera that I am activating and deactivating
Debug.Log ("$$anonymous$$OUSE");
Vector3 CamPos = Camera.main.transform.position;
FlythroughCameraController1 a;
FlythroughCameraControllerA b;
a=Camera.mainCamera.GetComponent<FlythroughCameraController1>();
b=Camera.mainCamera.GetComponent<FlythroughCameraControllerA>();
lerping = true;
}
IEnumerator myWait() {
Debug.Log ("myWait");
yield return new WaitForSeconds (2.0f);
}
// void OnApplicationQuit ()
// {
// FlythroughCameraController1 a;
// FlythroughCameraControllerA b;
// a=Camera.mainCamera.GetComponent<FlythroughCameraController1>();
// b=Camera.mainCamera.GetComponent<FlythroughCameraControllerA>();
// a.enabled = true;
// }
}
Sorry about all the updates. Had to fix a bunch of my own erros. xD
Thank you Aeleck! I stepped away and will look at this when I get back
I really appreciate your thoroughness & help!
best
~be
Your answer
Follow this Question
Related Questions
put Camera on path with touch &/or arrow keys 1 Answer
ITween orienttopath x-y-z? 1 Answer
iTween makes a weird start on the path 1 Answer
how to cancel iTween Catmull-Rom path 1 Answer
tween Path animation & touch control 1 Answer