- Home /
Question by
Newspapers · Jan 26, 2014 at 11:00 PM ·
camera2djavaverticalhorizontal
Vertically Stationary Camera for 2D?
I am working a 2D game, and I want to make it so the camera follows my player, but not in the vertical direction. That way the floor won't disappear if he jumps. Thanks for the help, the script (Java) is below).
var target : Transform;
var distance = 3.0;
var height = 3.0;
var damping = 5.0;
var smoothRotation = true;
var rotationDamping = 10.0;
var lockRotation : boolean;
function Update () {
var wantedPosition = target.TransformPoint (0, height, -distance);
transform.position = Vector3.Lerp (transform.position, wantedPosition, Time.deltaTime * damping);
if (smoothRotation) {
var wantedRotation = Quaternion.LookRotation(target.position - transform.position, target.up);
transform.rotation = Quaternion.Slerp (transform.rotation, wantedRotation, Time.deltaTime * rotationDamping);
}
else transform.LookAt (target, target.up);
if (lockRotation);{
transform.localRotation = Quaternion.EulerAngles(0,0,0);
}
}
Comment
If your jump is just vertical, have you tried something like
Update()
if(isjumping)
return;
Get the camera's y-position in Start() and store it in a variable. Then whenever you set the camera's position, in the line right after, set the camera's y coordinate to the one you stored.