A
A
Azmi2021-11-24 09:36:37
Unity
Azmi, 2021-11-24 09:36:37

How to setup camera for TopDown 2D game?

We have some kind of location, a character is moving on it, the camera is attached to it, moving accordingly, the camera has maximum and minimum values ​​so that textures outside the location are not shown, as soon as the character approaches one of the location boundaries, the camera stops, moreover, the character can go further, BUT if you change the resolution from 16:9 (1920x1080) to 4:3 (800x600), then the camera does not reach the border it needs, is it possible to solve this or is it easier to complete a couple of textures outside the location and set the maximum and minimum camera value is bigger

1920x1080
Game
619ddb80182a5391202435.jpeg
Scene
619ddb72c4ceb703192152.jpeg

800x600
Game
619ddbdec943a933936608.jpeg
Scene
619ddbd441e19393110323.jpeg

Short video - https://www.youtube.com/watch?v=AewMkNubhYA


Camera settings Camera
619ddc15aaea3600599859.png

script

public class CameraController : MonoBehaviour
{
    public Transform target;
    public float smoothing;
    public Vector2 maxPosition;
    public Vector2 minPosition;
    void Start()
    {
        transform.position = new Vector3(target.position.x,target.position.y,transform.position.z);
    }
    void LateUpdate()
    {
        if(transform.position != target.position)
        {
            Vector3 targetPosition = new Vector3(target.position.x, target.position.y, transform.position.z);
            targetPosition.x = Mathf.Clamp(targetPosition.x, minPosition.x, maxPosition.x);
            targetPosition.y = Mathf.Clamp(targetPosition.y, minPosition.y, maxPosition.y);
            transform.position = Vector3.Lerp(transform.position, targetPosition, smoothing);
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
Nekit Medvedev, 2021-11-25
@NIKROTOS

You have a minimum and maximum position for one resolution, and put another. Either set this data directly at once, or count at the start. By the way, targetPosition is better to create at the start

M
mustGamedev, 2021-11-24
@mustGamedev

Unity has a handy plugin for the camera - cinemachine.
It also allows you to limit the out of bounds space so that the character does not enter the game space.
Another way is easier.
You do not connect any plugins, you leave everything as it is. You just place invisible walls around the perimeter, beyond which the player will not go beyond the space. Profit

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question