- Home /
2D camera follow horizantal only?
so I'm fairly new to scripting and I got this script online 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 () {
target = GameObject.FindGameObjectWithTag("Player").transform;
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);
}
this makes the camera follow the player smoothly and I've been playing around with it for a while but I can't figure out how to add a variable that would make the camera only follow the target on the x axis. How would I do this? thank you.
Answer by drudiverse · Apr 19, 2014 at 04:13 PM
Easy... you want your camera to follow an object, but you want to control the xyz elements of the follow independently, just write a camera.position = Vector3(x = 2units (constant eyelevel), y = leading object.y (smooth with mathf. dampen function), z same as y)
or if you want it to move on x and other stuff say the same, control the same but visa versa