My camera script below works great at following the current player and keeping it within the bounds of my “walls” (empty game objects placed where I want the map to end).
However there is a jitter/shaking that happens when my player lands from a double jumps. The reason behind double jump (vs single jump) is that the camera actually moves up. Where as a normal jump the camera stays grounded to the bottom wall game object (basically my half screen buffer doesnt get hit… ‘delta’ I think).
How can I get rid of this shaking/jitter? To better explain “shaking/jitter” I would say its like a giant hitting the ground and causing it to shake (go up and down).
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
namespace TikiBeeGame {
class EndlessLevelCameraController : MonoBehaviour {
//public float speed = 2f;
public float dampTime = 0.15f;
private Vector3 velocity = Vector3.zero;
public Transform rightEndMarker = null;
public Transform leftEndMarker = null;
public Transform topEndMarker = null;
public Transform bottomEndMarker = null;
public bool useFixedUpdate = false;
private float leftWall = 0;
private float rightWall = 0;
private float bottomWall = 0;
private float topWall = 0;
void Start() {
}
void updateWalls() {
leftWall = camera.WorldToScreenPoint(leftEndMarker.position).x;
rightWall = camera.WorldToScreenPoint(rightEndMarker.position).x - Screen.width;
bottomWall = camera.WorldToScreenPoint(bottomEndMarker.position).y;
topWall = camera.WorldToScreenPoint(topEndMarker.position).y - Screen.height;
}
void LateUpdate() {
if (!useFixedUpdate)
updateCameraPosition();
}
void FixedUpdate() {
if (useFixedUpdate)
updateCameraPosition();
}
// Update is called once per frame
void updateCameraPosition() {
updateWalls();
if (PreferencesManager.CURRENT_PLAYER.transform) {
Vector3 delta = PreferencesManager.CURRENT_PLAYER.transform.position - camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0)); //(new Vector3(0.5, 0.5, point.z));
Vector3 destination = (transform.position + delta);
//destination = findBestPlacement(destination);
if (destination.x > rightWall) {
destination = new Vector3(rightWall, destination.y, 0);
}
if (destination.x < leftWall) {
destination = new Vector3(leftWall, destination.y, 0);
}
if (destination.y > topWall) {
destination = new Vector3(destination.x, topWall, 0);
}
if (destination.y < bottomWall) {
destination = new Vector3(destination.x, bottomWall, 0);
}
}
// here to always set the Z
destination = new Vector3(destination.x, destination.y, transform.position.z);
transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
}
}