@@ -15,7 +15,7 @@ public static CameraManager Instance{
1515 }
1616#region code-functions
1717 [ HideInInspector ] public UnityEvent < Transform > OnTargetChange = new UnityEvent < Transform > ( ) ;
18- [ HideInInspector ] public UnityEvent < float > OnZoom = new UnityEvent < float > ( ) ;
18+ [ HideInInspector ] public UnityEvent < float , float > OnCameraShake = new UnityEvent < float , float > ( ) ;
1919#endregion
2020
2121#region camera-variables
@@ -39,11 +39,8 @@ public Transform Target{
3939 }
4040 }
4141#endregion
42- #region zoom-variables
43- [ Header ( "Zoom settings" ) ]
44- [ SerializeField ] protected float zoomSpeed = 4f ;
45- protected float originalCameraSize ;
46- protected float zoomFactor ;
42+ #region shake-variables
43+ protected Coroutine shakeRoutine ;
4744#endregion
4845 /// <summary>
4946 /// This region will be focused on the basic camera functions as well as the workflow of it.
@@ -71,7 +68,6 @@ private void LateUpdate() {
7168 }
7269 protected virtual void Work ( ) {
7370 FollowTarget ( ) ;
74- HandleZoom ( ) ;
7571 }
7672 protected virtual void FollowTarget ( ) {
7773 if ( Target == null ) return ;
@@ -89,11 +85,20 @@ protected virtual void FollowTarget(){
8985 }
9086 this . transform . position = new Vector3 ( xNew , yNew , transform . position . z ) ;
9187 }
92- protected virtual void HandleZoom ( ) {
93- float targetSize = originalCameraSize - zoomFactor ;
94- if ( targetSize != currentCamera . orthographicSize ) {
95- currentCamera . orthographicSize = Mathf . Lerp ( currentCamera . orthographicSize , targetSize , Time . deltaTime * zoomSpeed ) ;
88+ protected virtual IEnumerator Shake ( float duration , float magnitude ) {
89+ Vector3 originalPos3 = currentCamera . transform . localPosition ;
90+ Vector2 originalPos = currentCamera . transform . localPosition ;
91+ float elapsedTime = 0f ;
92+ while ( elapsedTime < duration ) {
93+ currentCamera . transform . localPosition = new Vector3 ( originalPos . x + Random . insideUnitCircle . x * magnitude , originalPos . y + Random . insideUnitCircle . y * magnitude , - 10f ) ;
94+
95+ elapsedTime += Time . deltaTime ;
96+ yield return null ;
9697 }
98+
99+ currentCamera . transform . localPosition = originalPos3 ;
100+ //DefaultPosition();
101+ shakeRoutine = null ;
97102 }
98103#region publicfunctions
99104 //Region for every function that will be called from other scripts.
@@ -104,12 +109,16 @@ public virtual void SetTarget(Transform target, bool xLocked = false, bool yLock
104109 hasYLocked = yLocked ;
105110 OnTargetChange . Invoke ( target ) ;
106111 }
107- public virtual void SetZoom ( float zoomAmount , float zoomSpeed = 4f , string reason = "" ) {
108- if ( debug ) BDebug . Log ( $ "Camera is now zooming: { zoomAmount } , { reason } ", "Camera" , Color . green ) ;
109- zoomFactor = zoomAmount ;
110- this . zoomSpeed = zoomSpeed ;
111- OnZoom . Invoke ( zoomAmount ) ;
112+ public virtual void SetShake ( float duration , float magnitude , string reason = "" ) {
113+ if ( debug ) BDebug . Log ( $ "Camera is now shaking for { duration } seconds at { magnitude } X, { reason } ", "Camera" , Color . green ) ;
114+ if ( shakeRoutine != null ) {
115+ return ;
116+ }
117+ shakeRoutine = StartCoroutine ( Shake ( duration , magnitude ) ) ;
118+ OnCameraShake . Invoke ( duration , magnitude ) ;
112119 }
120+
121+
113122#endregion
114123 }
115124 public enum Updates {
0 commit comments