- Home /
Camera dolly/zoom to keep object on screen
I've been trying to set up a camera for a 2d game that moves on the x axis to follow an object, which moves on the x and y axis, and zooms out to keep it on screen (currently by dollying back on the z) and then back in.
The zoom out is smooth and works fine, but the zoom back in is choppy. Here is how it currently is: [[example]][1] (please excuse the silly music and placeholder everything)
How can I make this more of a smooth movement? (relevant code below)
var cam : Transform;
var object : Transform;
private var follow : boolean;
private var camX : float;
private var camY : float;
private var camZ : float;
private var objX : float;
private var camZStart : float;
private var screenPos : Vector3;
private var bufferOut : float;
private var bufferIn : float;
var bufferRatio : float;
var camMaxZ : float = 50;
private var camZTarget : float;
function Start()
{
camZStart = camera.transform.position.z;
bufferOut = Screen.height/bufferRatio;
bufferIn = Screen.height/bufferRatio;
camZTarget = camZStart;
}
function Update()
{
if (Launch.launch)
{
WaitForObj();//waits for object before moving horizontally
}
camX = camera.transform.position.x;
camY = camera.transform.position.y;
camZ = camera.transform.position.z;
objX = object.transform.position.x;
screenPos = camera.WorldToScreenPoint(object.transform.position);
if (Mathf.Abs(camZ) < camMaxZ){
if (screenPos.y > Screen.height-bufferOut && object.rigidbody.velocity.y > 0){
camZTarget = 1.02 * camZ;
}
}
if (camZ < camZStart){
if (screenPos.y < Screen.height-bufferIn && object.rigidbody.velocity.y <= 0){
camZTarget = 0.9 * camZ;
}
}
}
function WaitForObj()
{
if (follow == false && objX >= camX){
follow = true;
}
}
function LateUpdate()
{
if (follow){
transform.position.x = objX;
transform.position.y = camY;
}
transform.position.z = camZTarget;
}
[1]: http://html-bin.appspot.com/aghodG1sLWJpbnIMCxIEUGFnZRj1kAoM
Comment
Answer by Statement · Dec 26, 2011 at 01:46 PM
Maybe you could use a smoother function like Mathf.MoveTowards or Mathf.SmoothDamp?
float speed = 1.0f; // Adjust this to proper speed
transform.position.z = Mathf.MoveTowards(transform.position.z,
camZTarget, speed * Time.deltaTime);