- Home /
Stop main camera from zooming 2D
Hello everybody,
I am makeing a 2D platformer, where my camera follows my character. I made this by parenting the camera by my character and this works fine. But the problem is in the game the character is growing when it eats something, now everytime it grows the camera zooms further out. How could I stop this beahaveiour?
thanks for your help!!!
Answer by swanne · Nov 13, 2021 at 10:52 AM
Hey, You'll have to create camera follow logic via a script as opposed to nesting it under the player game object.
Take the camera back out of the player so it is not a child of any other object, then add this script called CameraFollow to the camera.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform target;
public float smoothSpeed = 0.125f;
public Vector3 offset;
private void FixedUpdate()
{
Vector3 desiredPosition = target.position + offset;
Vector3 smoothedPosition = Vector3.Lerp(transform.position, desiredPosition, smoothSpeed * Time.deltaTime);
transform.position = smoothedPosition;
transform.LookAt(target);
}
}
When that's done, assign the player as the target via the inspector and also set the offset that you'd like.