- Home /
Minimap in a 2D Game including level boundaries
Hi there,
I'm producing a 2D Sidescroller and I'm trying to implement a Minimap and have had some success.
However, the problem I'm coming up against is making it so the minimap doesn't show outside the level bounds.
With my main camera, I have this script, which stops the camera from following the player beyond the bounds of the level and so you never see the blue abyss behind it, however, if I apply this same script to the minimap camera, it shakes like crazy and won't follow the player (this problem also seems to occur with the main camera if the level it is on is narrower than the width of the camera).
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
public Transform target;
public Vector2 Margin;
public Vector2 Smoothing;
public BoxCollider2D Bounds;
private Vector3 _min;
private Vector3 _max;
public bool IsFollowing {get; set;}
void Start ()
{
Bounds = GameObject.FindGameObjectWithTag("Camera Bounds").GetComponent<BoxCollider2D>();
target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
_min = Bounds.bounds.min;
_max = Bounds.bounds.max;
IsFollowing = true;
}
void Update ()
{
if (target == null)
{
target = GameObject.Find("Character(Clone)").transform;
}
if(Bounds == null)
{
Bounds = GameObject.FindGameObjectWithTag("Camera Bounds").GetComponent<BoxCollider2D>();
}
var x = transform.position.x;
var y = transform.position.y;
if (IsFollowing)
{
if (Mathf.Abs (x - target.position.x) > Margin.x)
x = Mathf.Lerp (x, target.position.x, Smoothing.x * Time.deltaTime);
if (Mathf.Abs (y - target.position.y) > Margin.y)
y = Mathf.Lerp (y, target.position.y, Smoothing.y * Time.deltaTime);
}
var cameraHalfWidth = camera.orthographicSize * ((float) Screen.width / Screen.height);
x = Mathf.Clamp (x, _min.x + cameraHalfWidth, _max.x - cameraHalfWidth);
y = Mathf.Clamp (y, _min.y + camera.orthographicSize, _max.y - camera.orthographicSize);
transform.position = new Vector3(x, y, transform.position.z);
}
}
Does anyone have any suggestions on how I could go about making the minimap follow the player, but only to the edge of the level and not beyond (and also why this particular script makes the camera shake back and forward violently if the width of it is more than the width of the level)?
I've watched a load of tutorials and read a lot of questions, but I haven't found a single one relating to a minimap in a 2D game, nor one that handles not showing outside the level.
Your answer
Follow this Question
Related Questions
Minimap enemy icon help? 0 Answers
Move the camera in 2D when target reaches bounds. Is there a better way? 0 Answers
Change Player MiniMap Icon 1 Answer
Minimap Camera 2 Answers
Get mouse position relative to camera 2 Answers