I’m attempting to make a digicam system for a 2D platformer, with three digicam modes to start with:
Comply with, usually following the participant
Horizontal: Y is ready, X follows participant
Static: Locked to a sure place on the earth
After I change between modes, the digicam snaps to the brand new location instantly. I need it to pan as seen in video games like Hole Knight.
That is my code:
utilizing UnityEngine;
public class CameraFollowObject : MonoBehaviour
{
public CamMode mode { get; set; }
public Vector2 place { get; set; }
[Header("References")]
[SerializeField] non-public Rework playerTransform;
[Header("Flip Rotation Stats")]
[SerializeField] non-public float flipVRotationTime = 0.5f;
non-public Participant participant;
non-public bool isFacingRight;
non-public void Begin()
{
remodel.place = participant.remodel.place;
enabled = true;
participant = playerTransform.gameObject.GetComponent<Participant>();
isFacingRight = participant.motor.facingRight;
}
public void UpdateCamera()
{
Vector2 goal = mode change
{
CamMode.Horizontal => new Vector2(participant.remodel.place.x, place.y),
CamMode.Static => place,
_ => participant.remodel.place
};
remodel.place = goal;
}
public void CallTurn()
{
LeanTween.rotateY(gameObject, DetermineEndRotation(), flipVRotationTime).setEaseInOutSine();
}
non-public float DetermineEndRotation()
{
isFacingRight = !isFacingRight;
if (isFacingRight)
{
return 0f;
}
else
{
return 180f;
}
}
}
Digicam Comply with Object follows the participant and rotates on participant flip to easily pan from left bias to proper bias.
utilizing System.Collections;
utilizing UnityEngine;
utilizing Cinemachine;
public class CameraManager : MonoBehaviour
{
public static CameraManager occasion;
non-public CamMode currentMode;
[SerializeField] non-public CameraFollowObject followObject;
public bool isLerpingYDamping { get; non-public set; }
public bool lerpedFromPlayerFalling { get; set; }
non-public void Awake()
{
if (occasion == null)
{
occasion = this;
}
}
public void SwapCamera(CamMode left, CamMode proper, Vector2 exitDir, Vector2 pos)
{
if (currentMode == left && exitDir.x > 0f)
{
followObject.place = pos;
currentMode = followObject.mode = proper;
return;
}
if (currentMode == proper && exitDir.x < 0f)
{
followObject.place = pos;
currentMode = followObject.mode = left;
return;
}
}
}
Digicam Supervisor to modify Cameras
The Cameras are switched by triggers, all of it works completely high-quality, solely factor is, that the digicam immediately snaps. I attempted altering the remodel.place = goal; to lerp between present and goal, however that simply made the digicam fall behind when strolling.