- Home /
Question is off-topic or not relevant
ortographic camera smooth follow with zoom
at http://answers.unity3d.com/questions/29183/2d-camera-smooth-follow.html i found pretty simplet and really nice working script for camera follow
var dampTime : float = 0.3; //offset from the viewport center to fix damping
private var velocity = Vector3.zero;
var target : Transform;
function Update() {
if(target) {
var point : Vector3 = camera.WorldToViewportPoint(target.position);
var delta : Vector3 = target.position - camera.ViewportToWorldPoint(Vector3(0.5, 0.5, point.z));
var destination : Vector3 = transform.position + delta;
// Set this to the Y position you want the camera locked to
destination.y = 0;
transform.position = Vector3.SmoothDamp(transform.position, destination, velocity, dampTime);
}
}
it's almost perfect i just need put camera.ortographic size there to be able to zoom simultaneously so i tried to change Z coordinate to ortographic size and i can't findout why it's nor working (actually it's zooming out all the time)
here's my current script:
private void Update()
{
cameraPosition = new Vector3(target.x, target.y, camera.orthographicSize);
Vector3 difference = camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0f)) + Vector3.forward*camera.orthographicSize;
Vector3 delta = cameraPosition - difference;
Vector3 destinationMovement = cameraPosition + delta;
cameraPosition = Vector3.SmoothDamp(cameraPosition, destinationMovement, ref velocity, currentBoundaries.dampTimeMovement);
transform.position = new Vector3(cameraPosition.x, cameraPosition.y, transform.position.z);
camera.orthographicSize = cameraPosition.z;
}
Answer by zombience · Feb 21, 2014 at 04:30 PM
so, this is a bit of a hack, but one thing you might try is, if you know your final zoom points, create multiple cameras in your scene that have the zoom / view settings that you want.
for example, if you have a cube you're zooming in on, have a camera as a child of that cube, and arrange it so it is exactly placed where you want it, zoomed as you want. you can enable the camera in edit mode and preview it so it's set exactly as you want. you may then either disable the camera or have a script that disables the camera on game start.
then, when it is time to zoom to the cube, by some means or other (either GetComponentInChildren() or perhaps a general camera manager), grab the settings of that location camera and pass them to your current camera, and lerp to those new settings.
I've used this in a variety of scenarios and found it to be the best if you need very precise zoom locations. It even works on objects that are moving, since you can continually update the camera location to match the child camera which is focused exactly as you want it.
Hope that helps.
thank you for the suggestion, i definitely try it in some test project, but it's not my case - i have 2d scrolling game to do and only thing i need is to sync zoom with movement and fix bounding boxes for camera, right now it works but sometimes it makes weird stuff
Follow this Question
Related Questions
Cinemachine 2d camera problem 0 Answers
Camera script 1 Answer
IPhone is zoomed in/out depending on which model 0 Answers
Smooth Camera 2D Follow Player 0 Answers
Shaky player effect when I apply a damping camera follow script? 1 Answer