I’m making an attempt to make a digital camera system for a 2D platformer, with three digital camera modes to start with:
Observe, usually following the participant
Horizontal: Y is ready, X follows participant
Static: Locked to a sure place on the earth
After I swap between modes, the digital camera 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) personal Remodel playerTransform;
(Header("Flip Rotation Stats"))
(SerializeField) personal float flipVRotationTime = 0.5f;
personal Participant participant;
personal bool isFacingRight;
personal void Begin()
{
rework.place = participant.rework.place;
enabled = true;
participant = playerTransform.gameObject.GetComponent<Participant>();
isFacingRight = participant.motor.facingRight;
}
public void UpdateCamera()
{
Vector2 goal = mode swap
{
CamMode.Horizontal => new Vector2(participant.rework.place.x, place.y),
CamMode.Static => place,
_ => participant.rework.place
};
rework.place = goal;
}
public void CallTurn()
{
LeanTween.rotateY(gameObject, DetermineEndRotation(), flipVRotationTime).setEaseInOutSine();
}
personal float DetermineEndRotation()
{
isFacingRight = !isFacingRight;
if (isFacingRight)
{
return 0f;
}
else
{
return 180f;
}
}
}
Digital camera Observe 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;
personal CamMode currentMode;
(SerializeField) personal CameraFollowObject followObject;
public bool isLerpingYDamping { get; personal set; }
public bool lerpedFromPlayerFalling { get; set; }
personal 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;
}
}
}
Digital camera Supervisor to change Cameras
The Cameras are switched by triggers, all of it works completely positive, solely factor is, that the digital camera immediately snaps. I attempted altering the rework.place = goal; to lerp between present and goal, however that simply made the digital camera fall behind when strolling.