- Home /
The question is answered, right answer was accepted
2D Camera Collision
So I'm trying to make a game where you can move the camera within a set 2D Space, but I'm having trouble getting the camera to stop at walls. What I've done currently is made 2 scripts for the camera to test it:
public class BorderStop : MonoBehaviour {
static public bool hitTop = false;
static public bool hitBottom = false;
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Top"))
{
hitTop = true;
}
if (other.gameObject.CompareTag("Bottom"))
{
hitBottom = true;
}
}
void OnTriggerExit(Collider other)
{
if (other.gameObject.CompareTag("Top"))
{
hitTop = false;
}
if (other.gameObject.CompareTag("Bottom"))
{
hitBottom = false;
}
}
These Should be detecting when the camera (With its own Collider attached) hits a collider with the tag "Top" (The collider at the top of the Box) or "Bottom" (Collider at the bottom) This is the Cameras Script:
public class CameraController : MonoBehaviour {
public float speed;
public void FixedUpdate()
{
if (Input.GetKey(KeyCode.DownArrow) && BorderStop.hitBottom == false)
{
transform.position = new Vector3(transform.position.x, transform.position.y - speed, transform.position.z);
}
if (Input.GetKey(KeyCode.UpArrow) && BorderStop.hitTop == false)
{
transform.position = new Vector3(transform.position.x, transform.position.y + speed, transform.position.z);
}
}
}
So I'm not sure whats going wrong. Using Debug.Log I tested and it doesn't even detect the camera and border colliding. The Camera is simply free to move around and doesnt stop at the borders.
Answer by Petroglyph · Oct 23, 2017 at 10:59 PM
This has to do with how Colliders and Rigidbodies work in Unity and their relationship. Colliders on their own are meant to be static. They won't trigger anything with each other on their own. To specify that an object is not static, you should add a Rigidbody to it. Check the isKinematic option if you don't want it to be affected by forces and collisions. This will allow you to move it by code like you currently do!
I couldn't manage to get the collisions working, even though I can see the collision boxes overlapping in the editor, but I managed to make a workaround anyway. I realised that since I'm moving the camera around in a fixed space, I can simply use transform.position to test if it's at the border
if (Input.GetKey(KeyCode.DownArrow) && transform.position.y > -5)
{
transform.position = new Vector3(transform.position.x, transform.position.y - speed, transform.position.z);
}
if (Input.GetKey(KeyCode.UpArrow) && transform.position.y < 5)
{
transform.position = new Vector3(transform.position.x, transform.position.y + speed, transform.position.z);
}
This works fully and removes the need for any physics components or additional scripts.
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Display 1 No cameras rendering when player collides with enemy 1 Answer
Stop camera when wall hit 2D game 2 Answers
Modular Room System Similar to Mega Man 0 Answers