Z
Z
Zefirot2021-07-16 10:25:43
Unity
Zefirot, 2021-07-16 10:25:43

How to set the boundaries of the camera movement?

So far, I have set the min-max coordinates of the camera movement restrictions by eye, the movement itself happens like this

private void MoveCamera(){
        if(Input.GetMouseButtonDown(0)){ CurrentPositionCamera = MainCamera.ScreenToWorldPoint(Input.mousePosition); }
        if(Input.GetMouseButton(0)){
            Vector3 DifferencePositionCamera = CurrentPositionCamera - MainCamera.ScreenToWorldPoint(Input.mousePosition);
            if(DifferencePositionCamera != PrevPositionCamera){
                MainCamera.transform.position += DifferencePositionCamera;
                if(MainCamera.transform.position.x >= CameraPositionXMax){
                    MainCamera.transform.position = new Vector3(CameraPositionXMax, MainCamera.transform.position.y, 0f);
                    }
                if(MainCamera.transform.position.x <= CameraPositionXMin){
                    MainCamera.transform.position = new Vector3(CameraPositionXMin, MainCamera.transform.position.y, 0f);
                    }
                if(MainCamera.transform.position.y >= CameraPositionYMax){
                    MainCamera.transform.position = new Vector3(MainCamera.transform.position.x, CameraPositionYMax, 0f);
                    }
                if(MainCamera.transform.position.y <= CameraPositionYMin){
                    MainCamera.transform.position = new Vector3(MainCamera.transform.position.x, CameraPositionYMin, 0f);
                    }
                PrevPositionCamera = DifferencePositionCamera;
                }
            }
        }

But each room can have its own borders, further closer, while I have the option to write somewhere in the text docks under each room where what the border should be ...

Is it possible to somehow automatically determine the borders, what are the ways to do this?
Or is it possible to make some kind of prefab substrate so that it is not visible, but it can be adjusted and the camera could not automatically go beyond it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2021-07-16
@freeExec

Use BoxCollider, get bounds and it already has a method to check if a point (camera) falls into it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question