Skip to content

Instantly share code, notes, and snippets.

@Priler
Created July 30, 2021 09:01
Show Gist options
  • Save Priler/c1e0b661674e41e83d07635257d8f5b6 to your computer and use it in GitHub Desktop.
Save Priler/c1e0b661674e41e83d07635257d8f5b6 to your computer and use it in GitHub Desktop.
Geomtry Dash camera follow
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
[ExecuteInEditMode]
public class GD3DCameraFollow : MonoBehaviour
{
[SerializeField]
private Transform playerTransform;
[SerializeField]
private Vector3 cameraOffset;
[SerializeField]
private float deadZone = 5;
[SerializeField]
[Range(0.1f, 3f)]
private float tweenTime = 1f;
private float camY = 0;
private float targetCamY = 0;
private float baseCamY = 0;
private sbyte camStep = 1;
private bool isTweening = false;
// Update is called once per frame
void Update()
{
if( playerTransform.position.y > camStep * deadZone ) {
// camStep++ & lerp
targetCamY = camStep * deadZone;
camStep++;
baseCamY = camY;
} else if( playerTransform.position.y < (camStep * deadZone - deadZone / 2)) {
// camStep-- & lerp
camStep--;
targetCamY = camStep * deadZone;
baseCamY = camY;
}
if(camY != targetCamY && !isTweening) {
// tween
DOTween.To(() => baseCamY, x => camY = x, targetCamY, tweenTime).OnComplete(() => isTweening = false);
isTweening = true;
}
transform.position = new Vector3(playerTransform.position.x, camY, playerTransform.position.z) + cameraOffset;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment