- Home /
Question by
rodomvp · Mar 22, 2015 at 11:32 PM ·
camera2dcontroller
Camera becomes wonky after crossing the Y-Axis
Hey guys, I'm working through a 3dBuzz tutorial on 2D platformers (I'm an college student trying to pick up Unity and C#) and I encountered a problem working on the Camera Controller.
The camera tracks just fine however when the MainCamera object crosses the Y-Axis the entire Game view just reverts to the blue default background
I tried shifting the entire scene to the left side of the Y-Axis but then the camera just showed everything as blue.
the code for my Camera Controller is below
using UnityEngine;
using System.Collections;
public class CameraController : MonoBehaviour
{
public Transform Player;
public Vector2 Margin;
public Vector2 Smoothing;
public BoxCollider2D Bounds;
private Vector3
_min,
_max;
public bool IsFollowing { get; set; }
public void Start()
{
_min = Bounds.bounds.min;
_max = Bounds.bounds.max;
IsFollowing = true;
}
public void Update()
{
var x = transform.position.x;
var y = transform.position.y;
if (IsFollowing)
{
if(Mathf.Abs(x - Player.position.x) > Margin.x)
x = Mathf.Lerp(x, Player.position.x, Smoothing.x * Time.deltaTime);
if(Mathf.Abs(y - Player.position.y) > Margin.y)
y = Mathf.Lerp(y, Player.position.y, Smoothing.y * Time.deltaTime);
}
var cameraHalfWidth = Camera.main.orthographicSize * ((float) Screen.width / Screen.height);
x = Mathf.Clamp(x, _min.x + cameraHalfWidth, _max.x - cameraHalfWidth);
y = Mathf.Clamp(y, _min.y + Camera.main.orthographicSize, _max.y - Camera.main.orthographicSize);
transform.position = new Vector3(x, y, transform.position.x);
}
}
Comment