Skip to content

Commit 70032f8

Browse files
authored
fix: Created the first version of the camera manager
fully finished the base version for the camera manager with following and shake actions
2 parents eb335d5 + f9e4aec commit 70032f8

File tree

5 files changed

+254
-5
lines changed

5 files changed

+254
-5
lines changed

Runtime/Camera.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Camera/CameraManager.cs

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using UnityEngine.Events;
5+
6+
namespace BigasTools{
7+
public class CameraManager : MonoBehaviour
8+
{
9+
private static CameraManager instance;
10+
public static CameraManager Instance{
11+
get{
12+
if(instance == null)instance = FindObjectOfType<CameraManager>();
13+
return instance;
14+
}
15+
}
16+
#region code-functions
17+
[HideInInspector] public UnityEvent<Transform> OnTargetChange = new UnityEvent<Transform>();
18+
[HideInInspector] public UnityEvent<float, float> OnCameraShake = new UnityEvent<float, float>();
19+
#endregion
20+
21+
#region camera-variables
22+
[SerializeField] bool debug;
23+
24+
[Header("Camera settings")]
25+
[SerializeField] Updates workOn = Updates.LATE;
26+
[SerializeField] Transform cameraHolder;
27+
[SerializeField] Camera currentCamera;
28+
[SerializeField] float followSpeed = 4f;
29+
[SerializeField] Vector3 offset;
30+
public bool hasXLocked = false;
31+
public bool hasYLocked = false;
32+
protected Transform target;
33+
34+
public Transform Target{
35+
get{
36+
return target;
37+
}set{
38+
target = value;
39+
}
40+
}
41+
#endregion
42+
#region shake-variables
43+
protected Coroutine shakeRoutine;
44+
#endregion
45+
/// <summary>
46+
/// This region will be focused on the basic camera functions as well as the workflow of it.
47+
/// </summary>
48+
private void Awake() {
49+
OnAwake();
50+
}
51+
protected virtual void OnAwake(){
52+
if(debug)BDebug.Log($"Starting camera at position: {this.transform.position}", "Camera", Color.green);
53+
}
54+
private void Update() {
55+
if(workOn == Updates.UPDATE){
56+
Work();
57+
}
58+
}
59+
private void FixedUpdate() {
60+
if(workOn == Updates.FIXED){
61+
Work();
62+
}
63+
}
64+
private void LateUpdate() {
65+
if(workOn == Updates.LATE){
66+
Work();
67+
}
68+
}
69+
protected virtual void Work(){
70+
FollowTarget();
71+
}
72+
protected virtual void FollowTarget(){
73+
if(Target == null)return;
74+
float xTarget = Target.position.x + offset.x;
75+
float yTarget = Target.position.y + offset.y;
76+
//float zTarget = Target.position.z + offset.z;
77+
78+
float xNew = this.transform.position.x;
79+
if(!hasXLocked){
80+
xNew = Mathf.Lerp(this.transform.position.x, xTarget, Time.deltaTime*followSpeed);
81+
}
82+
float yNew = this.transform.position.y;
83+
if(!hasYLocked){
84+
yNew = Mathf.Lerp(this.transform.position.y, yTarget, Time.deltaTime*followSpeed);
85+
}
86+
this.transform.position = new Vector3(xNew, yNew, transform.position.z);
87+
}
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;
97+
}
98+
99+
currentCamera.transform.localPosition = originalPos3;
100+
//DefaultPosition();
101+
shakeRoutine = null;
102+
}
103+
#region publicfunctions
104+
//Region for every function that will be called from other scripts.
105+
public virtual void SetTarget(Transform target, bool xLocked = false, bool yLocked = false, string reason = ""){
106+
if(debug)BDebug.Log($"Camera is now following: {target}, {reason}", "Camera", Color.green);
107+
Target = target;
108+
hasXLocked = xLocked;
109+
hasYLocked = yLocked;
110+
OnTargetChange.Invoke(target);
111+
}
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);
119+
}
120+
121+
122+
#endregion
123+
}
124+
public enum Updates{
125+
UPDATE,
126+
FIXED,
127+
LATE
128+
}
129+
}

Runtime/Camera/CameraManager.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Samples/EntityExamples.unity

Lines changed: 95 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,37 @@ CanvasRenderer:
301301
m_PrefabAsset: {fileID: 0}
302302
m_GameObject: {fileID: 694918877}
303303
m_CullTransparentMesh: 1
304+
--- !u!1 &1175543323
305+
GameObject:
306+
m_ObjectHideFlags: 0
307+
m_CorrespondingSourceObject: {fileID: 0}
308+
m_PrefabInstance: {fileID: 0}
309+
m_PrefabAsset: {fileID: 0}
310+
serializedVersion: 6
311+
m_Component:
312+
- component: {fileID: 1175543324}
313+
m_Layer: 0
314+
m_Name: Holder
315+
m_TagString: Untagged
316+
m_Icon: {fileID: 0}
317+
m_NavMeshLayer: 0
318+
m_StaticEditorFlags: 0
319+
m_IsActive: 1
320+
--- !u!4 &1175543324
321+
Transform:
322+
m_ObjectHideFlags: 0
323+
m_CorrespondingSourceObject: {fileID: 0}
324+
m_PrefabInstance: {fileID: 0}
325+
m_PrefabAsset: {fileID: 0}
326+
m_GameObject: {fileID: 1175543323}
327+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
328+
m_LocalPosition: {x: 0, y: 0, z: 0}
329+
m_LocalScale: {x: 1, y: 1, z: 1}
330+
m_Children:
331+
- {fileID: 1696512267}
332+
m_Father: {fileID: 1721610021}
333+
m_RootOrder: 0
334+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
304335
--- !u!1 &1696512264
305336
GameObject:
306337
m_ObjectHideFlags: 0
@@ -378,11 +409,11 @@ Transform:
378409
m_PrefabInstance: {fileID: 0}
379410
m_PrefabAsset: {fileID: 0}
380411
m_GameObject: {fileID: 1696512264}
381-
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
412+
m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
382413
m_LocalPosition: {x: 0, y: 0, z: -10}
383414
m_LocalScale: {x: 1, y: 1, z: 1}
384415
m_Children: []
385-
m_Father: {fileID: 0}
416+
m_Father: {fileID: 1175543324}
386417
m_RootOrder: 0
387418
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
388419
--- !u!114 &1696512268
@@ -405,6 +436,65 @@ MonoBehaviour:
405436
m_CropFrameX: 0
406437
m_CropFrameY: 0
407438
m_StretchFill: 0
439+
--- !u!1 &1721610020
440+
GameObject:
441+
m_ObjectHideFlags: 0
442+
m_CorrespondingSourceObject: {fileID: 0}
443+
m_PrefabInstance: {fileID: 0}
444+
m_PrefabAsset: {fileID: 0}
445+
serializedVersion: 6
446+
m_Component:
447+
- component: {fileID: 1721610021}
448+
- component: {fileID: 1721610022}
449+
m_Layer: 0
450+
m_Name: CameraManager
451+
m_TagString: Untagged
452+
m_Icon: {fileID: 0}
453+
m_NavMeshLayer: 0
454+
m_StaticEditorFlags: 0
455+
m_IsActive: 1
456+
--- !u!4 &1721610021
457+
Transform:
458+
m_ObjectHideFlags: 0
459+
m_CorrespondingSourceObject: {fileID: 0}
460+
m_PrefabInstance: {fileID: 0}
461+
m_PrefabAsset: {fileID: 0}
462+
m_GameObject: {fileID: 1721610020}
463+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
464+
m_LocalPosition: {x: 0, y: 0, z: 0}
465+
m_LocalScale: {x: 1, y: 1, z: 1}
466+
m_Children:
467+
- {fileID: 1175543324}
468+
m_Father: {fileID: 0}
469+
m_RootOrder: 3
470+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
471+
--- !u!114 &1721610022
472+
MonoBehaviour:
473+
m_ObjectHideFlags: 0
474+
m_CorrespondingSourceObject: {fileID: 0}
475+
m_PrefabInstance: {fileID: 0}
476+
m_PrefabAsset: {fileID: 0}
477+
m_GameObject: {fileID: 1721610020}
478+
m_Enabled: 1
479+
m_EditorHideFlags: 0
480+
m_Script: {fileID: 11500000, guid: e3a9fe62ea8839d4ea3c402ba4ea2ad4, type: 3}
481+
m_Name:
482+
m_EditorClassIdentifier:
483+
OnTargetChange:
484+
m_PersistentCalls:
485+
m_Calls: []
486+
OnZoom:
487+
m_PersistentCalls:
488+
m_Calls: []
489+
debug: 1
490+
workOn: 1
491+
cameraHolder: {fileID: 1175543324}
492+
currentCamera: {fileID: 1696512266}
493+
followSpeed: 4
494+
offset: {x: 0, y: 0, z: 0}
495+
hasXLocked: 0
496+
hasYLocked: 0
497+
zoomSpeed: 4
408498
--- !u!1 &1742347587
409499
GameObject:
410500
m_ObjectHideFlags: 0
@@ -498,7 +588,7 @@ RectTransform:
498588
m_Children:
499589
- {fileID: 694918878}
500590
m_Father: {fileID: 0}
501-
m_RootOrder: 3
591+
m_RootOrder: 2
502592
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
503593
m_AnchorMin: {x: 0, y: 0}
504594
m_AnchorMax: {x: 0, y: 0}
@@ -569,7 +659,7 @@ Transform:
569659
m_LocalScale: {x: 1, y: 1, z: 1}
570660
m_Children: []
571661
m_Father: {fileID: 0}
572-
m_RootOrder: 1
662+
m_RootOrder: 0
573663
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
574664
--- !u!1 &2027691190
575665
GameObject:
@@ -621,7 +711,7 @@ Transform:
621711
m_LocalScale: {x: 1, y: 1, z: 1}
622712
m_Children: []
623713
m_Father: {fileID: 0}
624-
m_RootOrder: 2
714+
m_RootOrder: 1
625715
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
626716
--- !u!114 &2027691193
627717
MonoBehaviour:

Tests/Runtime/EntityExample.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,29 @@ public class EntityExample : MonoBehaviour
1111
[SerializeField] Entity entityToSpawn;
1212
List<Entity> entitysOnExample = new List<Entity>();
1313
float lootTableTimer = 0;
14+
private void Start() {
15+
CameraManager.Instance.OnTargetChange.AddListener((target)=>{
16+
Debug.Log(target);
17+
});
18+
CameraManager.Instance.OnCameraShake.AddListener((d,m)=>{
19+
Debug.Log(d+m);
20+
});
21+
}
1422
private void Update() {
1523
if(BGameInput.Instance.GetKeyPress("Interaction")){
1624
var e = Instantiate(entityToSpawn);
1725
e.transform.position = new Vector2(Random.Range(-13, 13), Random.Range(-8, 8));
1826
e.onDispose += () =>{
1927
entitysOnExample.Remove(e);
2028
};
29+
CameraManager.Instance.SetTarget(e.transform, false, false, $"Changing to {e}");
2130
entitysOnExample.Add(e);
2231
}
2332
if(BGameInput.Instance.GetKeyPress("Example")){
2433
var rnd = Random.Range(0, entitysOnExample.Count - 1);
2534

35+
CameraManager.Instance.SetShake(.5f, 1.5f, "Testing the zoom!");
36+
2637
entitysOnExample[rnd].MoveTo(new Vector2(1,0));
2738
}
2839
if(BGameInput.Instance.GetKeyPress("Example1")){

0 commit comments

Comments
 (0)