Post by davidc02 on Feb 5, 2019 21:08:49 GMT
Hey, I made this script to adjust the camera window’s height when the character is grounded. This so when the character touches the ground the bottom of the camera window will adjust to the position of the character’s feet. Wanted to check if it looked accurate and share in case anyone wants to use this behavior/script, feel free to do so.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Com.LuisPedroFonseca.ProCamera2D;
namespace Com.LuisPedroFonseca.ProCamera2D
{
/// <summary>
/// Camera Grounded Behaviour is used so the camera's window is adjusted while the character is grounded.
/// </summary>
public class CameraGroundedBehaviour : BasePC2D
{
protected IMoveable _target;
public float groundedRate;
public float lerpEpsilon;
public float cameraWindowHeight;
protected virtual void Start()
{
Initialize();
cameraWindowHeight = ProCamera2D.GetComponent<ProCamera2DCameraWindow>().CameraWindowRect.height;
}
protected virtual void Initialize()
{
_target = GameObject.Find("Ebba").GetComponent<IMoveable>();
}
void Update () {
AdjustDeadzone();
}
/// <summary>
/// Adjust Camera Window.
/// </summary>
private void AdjustDeadzone()
{
if(_target.Ground) //|| _target.Velocity.y < 1
{
float lerpValue = Mathf.Lerp(ProCamera2D.GetComponent<ProCamera2DCameraWindow>().CameraWindowRect.height, 0.0f, Time.deltaTime * groundedRate);
if (MathUtils.Approximately(lerpValue, 0.01f, lerpEpsilon))
ProCamera2D.GetComponent<ProCamera2DCameraWindow>().CameraWindowRect.height = 0;
else
ProCamera2D.GetComponent<ProCamera2DCameraWindow>().CameraWindowRect.height = lerpValue;
}
else
{
ProCamera2D.GetComponent<ProCamera2DCameraWindow>().CameraWindowRect.height = cameraWindowHeight;
}
}
}
}