- Home /
Limiting camera to visible scene
I have a 2D map consisting of sprite GameObjects.
I have set up my camera so I can move it w arrows but i'm not sure how I should limit the camera to the borders of visible scene(so the user only sees actual tiles)
What I wanted is that the camera stops at the scene edges, but I'm not sure how to do this. Since I wrote a script that generates the 'game map' based on a number for columns/rows, I would like to have dynamic borders based on the scene's dimensions
this is my code so far:
using UnityEngine; using System.Collections;
public class CameraControl : MonoBehaviour {
public float speed = 1.5f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKey(KeyCode.RightArrow))
{
transform.Translate(new Vector2(speed * Time.deltaTime,0.0f));
}
if(Input.GetKey(KeyCode.LeftArrow))
{
transform.Translate (new Vector2 (-speed * Time.deltaTime, 0.0f));
}
if(Input.GetKey(KeyCode.DownArrow))
{
transform.Translate (new Vector2 (0.0f, -speed * Time.deltaTime));
}
if(Input.GetKey(KeyCode.UpArrow))
{
transform.Translate (new Vector2 (0.0f, speed * Time.deltaTime));
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612100655im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
camera script not working 1 Answer
How to make the camera acquire the position of a gameobject and start following it? 1 Answer
Cinemachine camera shake on button press 0 Answers
How can I get my camera to only follow my player down along the Y axis? 2 Answers
Camera Movement and Zooming limit, 2 Answers