- Home /
Camera Shaking Problem
Hi there,
I use the script below to make my camera follow my player in a 2D Sidescroller, but only within the bounds of the level (so you don't see the skybox behind etc).
The problem I'm having is that on certain maps (if they're quite narrow on the x-axis) the camera will show some of the skybox, and when you move to a certain point it shakes back and forward like it's bouncing between the level bounds. Has anyone experienced this kind of problem and/or know how to fix it?
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 Awake()
{
if(spawned == false)
{
spawned = true;
DontDestroyOnLoad(gameObject);
}
else
{
DestroyImmediate(gameObject);
}
}*/
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);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613183743im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Camera does not resume to original Position after Shake 1 Answer
Set the default 6d shake noise profile by script in Cinemachine? 0 Answers
The base of every gameobject shake 1 Answer
Unity 2D Position Issues 0 Answers
Shaking camera 0 Answers