Skip to content

Instantly share code, notes, and snippets.

@PixelEnvision
Created February 7, 2023 12:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PixelEnvision/b6ff5d1130a5ec657754b6e97d577f28 to your computer and use it in GitHub Desktop.
Save PixelEnvision/b6ff5d1130a5ec657754b6e97d577f28 to your computer and use it in GitHub Desktop.
A Cinemachine Extension to keep the camera above ground.
using UnityEngine;
using Cinemachine;
[AddComponentMenu("")] // Hide in menu
[SaveDuringPlay]
#if UNITY_2018_3_OR_NEWER
[ExecuteAlways]
#else
[ExecuteInEditMode]
#endif
public class CinemachineGroundChecker : CinemachineExtension
{
[SerializeField] LayerMask groundLayer;
[SerializeField] float AdjustmentSpeed = 5;
RaycastHit hit;
Vector3 initialOffset;
Vector3 offset;
//float offsetY;
protected override void Awake()
{
base.Awake();
initialOffset = GetComponent<CinemachineVirtualCamera>().GetCinemachineComponent<CinemachineTransposer>().m_FollowOffset;
initialOffset.z = 0;
}
protected override void PostPipelineStageCallback(
CinemachineVirtualCameraBase vcam,
CinemachineCore.Stage stage, ref CameraState state, float deltaTime)
{
if (stage == CinemachineCore.Stage.Body && groundLayer.value != 0)
{
var camPos = state.CorrectedPosition;
camPos.y += initialOffset.y;
//Debug.DrawRay(camPos, Vector3.down * initialOffset);
//Debug.DrawRay(camPos + (Vector3.down * initialOffset), Vector3.down * initialOffset, Color.red);
if (Physics.Raycast(camPos, -state.ReferenceUp, out hit, initialOffset.y * 2, groundLayer, QueryTriggerInteraction.Ignore))
{
//offsetY = Mathf.Lerp(offsetY, initialOffset.y - (state.CorrectedPosition.y - hit.point.y), deltaTime * AdjustmentSpeed);
offset = Vector3.Lerp(offset, initialOffset - (state.CorrectedPosition - hit.point), deltaTime * AdjustmentSpeed);
}
else
{
//offsetY = Mathf.Lerp(offsetY, 0, deltaTime * 0.5f);
offset = Vector3.Lerp(offset, Vector3.zero, deltaTime * 0.5f);
}
//if (offsetY >= 0)
// state.PositionCorrection += new Vector3(0, offsetY, 0);
state.PositionCorrection += offset;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment