- Home /
How can I make a smooth camera transition over time with java script?
I'm doing a side scroller platform game and have two scripts for the camera. One makes it work as a side scroller camera, maintaining it's distance and following the character, and the other makes the camera go farther away from the character (so you can see more of the level) when entering a trigger, and back to it's original position when exiting the trigger:
#pragma strict
var Target : Transform;
static var distanceFromPlayer = 10;
function Start () {
}
function Update () {
}
function LateUpdate () {
transform.position = Target.position + Vector3(distanceFromPlayer,0,0);
}
Side scroller camera
function Start(){
}
function OnTriggerStay(other : Collider){
sidescrollerCameraScript.distanceFromPlayer = 18;
}
function OnTriggerExit(other : Collider){
sidescrollerCameraScript.distanceFromPlayer = 10;
}
Triggered camera switch:
Both work fine, but the switch of the camera looks too snappy, taking one frame to change positions. so I tried making it look smoother using the yield command like this:
function OnTriggerStay(other : Collider){
sidescrollerCaemeraScript.distanceFromPlayer = 11;
yield WaitForSeconds(0.2);
sidescrollerCaemraScript.distanceFromPlayer = 12;
yield WaitForSeconds(0.2);
sidescrollerCameraScript.distanceFromPlayer = 13;
yield WaitForSeconds(0.2);
sidescrollerCameraScript.distanceFromPlayer = 14;
yield WaitForSeconds(0.2);
sidescrollerCameraScript.distanceFromPlayer = 15;
yield WaitForSeconds(0.2);
sidescrollerCameraScript.distanceFromPlayer = 16;
yield WaitForSeconds(0.2);
sidescrollerCameraScript.distanceFromPlayer = 17;
yield WaitForSeconds(0.2);
sidescrollerCameraScript.distanceFromPlayer = 18;
}
But this made unity confused and instead of following through it makes the camera go back and ford multiple times in a fraction of a second when inside the trigger, not what I intended.
So is there a way to affect the variable over time from point A all the way to point B so the camera transition looks smoother?
Answer by Cherno · Oct 09, 2013 at 11:27 PM
Just do it as you would make any other object move, with a while / for loop: var StartPosition : Vector3 = transform.position; var EndPosition : Vector3;
var t : float = 0.0;
while (t < 1.0)
{
t += Time.deltaTime;
transform.position = Vector3.Lerp(StartPosition, EndPosition, t);
yield;
}
obviously you use your required distanceFromPlayer variables instead of Vector3s.
so if I understand correctly, my code should look like this?:
while (t < 1.0)
{
t += Time.deltaTime;
sidescrolerCameraScript.distanceFromPlayer = sidescrolerCameraScript.distanceFromPlayer(10, 18, t);
}
or am I wrong?
ok no Im wrong, could you show me an example of how to work it with my distanceFromPlayer variable?
var $$anonymous$$axDistance : float = 20;
var Player : GameObject;//drag your player object here
var CameraSpeed : float = 0.5;
function $$anonymous$$oveCamera()
{
while (Vector3.Distance(transform.position, Player.transform.position) < $$anonymous$$axDistance)
{
transform.Translate(-Vector3.forward * CameraSpeed);
yield;
}
}