From 9064dab293bf94ee3e3aa9a08c6256efae6164a1 Mon Sep 17 00:00:00 2001 From: MrGadget <9826063+MrGadget1024@users.noreply.github.com> Date: Sat, 10 Aug 2024 14:43:29 -0400 Subject: [PATCH] fix(CharacterSelection Example): Separate SceneCamera script (#3888) * CharSelection: Separate SceneCamera script * SceneCamera: SetPositionAndRotation * Code Cleanup --- .../Characters/CharacterAssault.prefab | 21 ++++++- .../Prefabs/Characters/CharacterHeavy.prefab | 21 ++++++- .../Prefabs/Characters/CharacterMedic.prefab | 21 ++++++- .../Scripts/PlayerControllerScript.cs | 29 +-------- .../CharacterSelection/Scripts/SceneCamera.cs | 59 +++++++++++++++++++ .../Scripts/SceneCamera.cs.meta | 11 ++++ 6 files changed, 132 insertions(+), 30 deletions(-) create mode 100644 Assets/Mirror/Examples/CharacterSelection/Scripts/SceneCamera.cs create mode 100644 Assets/Mirror/Examples/CharacterSelection/Scripts/SceneCamera.cs.meta diff --git a/Assets/Mirror/Examples/CharacterSelection/Prefabs/Characters/CharacterAssault.prefab b/Assets/Mirror/Examples/CharacterSelection/Prefabs/Characters/CharacterAssault.prefab index 374443871..d68bf0187 100644 --- a/Assets/Mirror/Examples/CharacterSelection/Prefabs/Characters/CharacterAssault.prefab +++ b/Assets/Mirror/Examples/CharacterSelection/Prefabs/Characters/CharacterAssault.prefab @@ -1404,6 +1404,7 @@ GameObject: - component: {fileID: 3603318197913287424} - component: {fileID: -5286820321314269966} - component: {fileID: 108717044788610704} + - component: {fileID: 8165614180483664709} m_Layer: 0 m_Name: CharacterAssault m_TagString: Untagged @@ -1541,7 +1542,6 @@ MonoBehaviour: animRotation: 0 velocity: {x: 0, y: 0, z: 0} direction: {x: 0, y: 0, z: 0} - cameraTarget: {fileID: 8318204445083151948} --- !u!114 &-5286820321314269966 MonoBehaviour: m_ObjectHideFlags: 0 @@ -1588,6 +1588,25 @@ MonoBehaviour: - {fileID: 3074504445760471458} - {fileID: 3074504446469250245} - {fileID: 3074504445848950737} +--- !u!114 &8165614180483664709 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 3074504447143314537} + m_Enabled: 0 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: db9418a9e709a7844acac42de096cba6, type: 3} + m_Name: + m_EditorClassIdentifier: + syncDirection: 0 + syncMode: 0 + syncInterval: 0 + characterSelection: {fileID: 108717044788610704} + cameraTarget: {fileID: 8318204445083151948} + sceneReferencer: {fileID: 0} + cameraObj: {fileID: 0} --- !u!1 &3074504447184887503 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Mirror/Examples/CharacterSelection/Prefabs/Characters/CharacterHeavy.prefab b/Assets/Mirror/Examples/CharacterSelection/Prefabs/Characters/CharacterHeavy.prefab index 84e4c5be5..99b6b012f 100644 --- a/Assets/Mirror/Examples/CharacterSelection/Prefabs/Characters/CharacterHeavy.prefab +++ b/Assets/Mirror/Examples/CharacterSelection/Prefabs/Characters/CharacterHeavy.prefab @@ -343,6 +343,7 @@ GameObject: - component: {fileID: -7643125540303550518} - component: {fileID: -7784293741018888422} - component: {fileID: 597190161414468650} + - component: {fileID: -6486408998751470869} m_Layer: 0 m_Name: CharacterHeavy m_TagString: Untagged @@ -480,7 +481,6 @@ MonoBehaviour: animRotation: 0 velocity: {x: 0, y: 0, z: 0} direction: {x: 0, y: 0, z: 0} - cameraTarget: {fileID: 5721588646566991880} --- !u!114 &-7784293741018888422 MonoBehaviour: m_ObjectHideFlags: 0 @@ -524,6 +524,25 @@ MonoBehaviour: - {fileID: 2320049527593028056} - {fileID: 2320049526879068635} - {fileID: 2320049528211028347} +--- !u!114 &-6486408998751470869 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 2320049526911491159} + m_Enabled: 0 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: db9418a9e709a7844acac42de096cba6, type: 3} + m_Name: + m_EditorClassIdentifier: + syncDirection: 0 + syncMode: 0 + syncInterval: 0 + characterSelection: {fileID: 597190161414468650} + cameraTarget: {fileID: 5721588646566991880} + sceneReferencer: {fileID: 0} + cameraObj: {fileID: 0} --- !u!1 &2320049527168386026 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Mirror/Examples/CharacterSelection/Prefabs/Characters/CharacterMedic.prefab b/Assets/Mirror/Examples/CharacterSelection/Prefabs/Characters/CharacterMedic.prefab index a042ab538..7fd30b8ed 100644 --- a/Assets/Mirror/Examples/CharacterSelection/Prefabs/Characters/CharacterMedic.prefab +++ b/Assets/Mirror/Examples/CharacterSelection/Prefabs/Characters/CharacterMedic.prefab @@ -778,6 +778,7 @@ GameObject: - component: {fileID: -4474909813393930685} - component: {fileID: 3540726253812090381} - component: {fileID: 4511077071076738942} + - component: {fileID: 958723225240916811} m_Layer: 0 m_Name: CharacterMedic m_TagString: Untagged @@ -915,7 +916,6 @@ MonoBehaviour: animRotation: 0 velocity: {x: 0, y: 0, z: 0} direction: {x: 0, y: 0, z: 0} - cameraTarget: {fileID: 2977242866262118958} --- !u!114 &3540726253812090381 MonoBehaviour: m_ObjectHideFlags: 0 @@ -958,6 +958,25 @@ MonoBehaviour: - {fileID: 9105303183086384159} - {fileID: 9105303183046110706} - {fileID: 9105303183986044559} +--- !u!114 &958723225240916811 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 9105303183089727057} + m_Enabled: 0 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: db9418a9e709a7844acac42de096cba6, type: 3} + m_Name: + m_EditorClassIdentifier: + syncDirection: 0 + syncMode: 0 + syncInterval: 0 + characterSelection: {fileID: 4511077071076738942} + cameraTarget: {fileID: 2977242866262118958} + sceneReferencer: {fileID: 0} + cameraObj: {fileID: 0} --- !u!1 &9105303183937996607 GameObject: m_ObjectHideFlags: 0 diff --git a/Assets/Mirror/Examples/CharacterSelection/Scripts/PlayerControllerScript.cs b/Assets/Mirror/Examples/CharacterSelection/Scripts/PlayerControllerScript.cs index 24cf59dc5..a12af7c0f 100644 --- a/Assets/Mirror/Examples/CharacterSelection/Scripts/PlayerControllerScript.cs +++ b/Assets/Mirror/Examples/CharacterSelection/Scripts/PlayerControllerScript.cs @@ -55,18 +55,10 @@ public enum GroundState : byte { Jumping, Falling, Grounded } [ReadOnly, SerializeField] Vector3Int velocity; [ReadOnly, SerializeField] Vector3 direction; - private Transform cameraObj; - public Transform cameraTarget; - private SceneReferencer sceneReferencer; - private CharacterSelection characterSelection; - public void Awake() { - if (characterController == null) characterController = GetComponent(); - if (characterSelection == null) - characterSelection = GetComponent(); // Override CharacterController default values characterController.enabled = false; @@ -82,14 +74,6 @@ public override void OnStartAuthority() { characterController.enabled = true; this.enabled = true; - -#if UNITY_2022_2_OR_NEWER - sceneReferencer = GameObject.FindAnyObjectByType(); -#else - // Deprecated in Unity 2023.1 - sceneReferencer = GameObject.FindObjectOfType(); -#endif - cameraObj = sceneReferencer.cameraObject.transform; } public override void OnStopAuthority() @@ -100,12 +84,9 @@ public override void OnStopAuthority() void Update() { - if (cameraObj && characterSelection) - { - characterSelection.floatingInfo.forward = cameraObj.transform.forward; - } + if (!Application.isFocused) + return; - if (!Application.isFocused) return; if (!characterController.enabled) return; @@ -121,12 +102,6 @@ void Update() // Diagnostic velocity...FloorToInt for display purposes velocity = Vector3Int.FloorToInt(characterController.velocity); - - if (cameraObj != null) - { - cameraObj.position = cameraTarget.position; - cameraObj.rotation = cameraTarget.rotation; - } } // TODO: Turning works while airborne...feature? diff --git a/Assets/Mirror/Examples/CharacterSelection/Scripts/SceneCamera.cs b/Assets/Mirror/Examples/CharacterSelection/Scripts/SceneCamera.cs new file mode 100644 index 000000000..6c3ac3986 --- /dev/null +++ b/Assets/Mirror/Examples/CharacterSelection/Scripts/SceneCamera.cs @@ -0,0 +1,59 @@ +using UnityEngine; + +namespace Mirror.Examples.CharacterSelection +{ + public class SceneCamera : NetworkBehaviour + { + [Header("Components")] + [SerializeField] CharacterSelection characterSelection; + [SerializeField] Transform cameraTarget; + + [Header("Diagnostics")] + [ReadOnly, SerializeField] SceneReferencer sceneReferencer; + [ReadOnly, SerializeField] Transform cameraObj; + + protected override void OnValidate() + { + base.OnValidate(); + Reset(); + } + + void Reset() + { + characterSelection = GetComponent(); + cameraTarget = transform.Find("CameraTarget"); + this.enabled = false; + } + + public override void OnStartAuthority() + { +#if UNITY_2022_2_OR_NEWER + sceneReferencer = GameObject.FindAnyObjectByType(); +#else + // Deprecated in Unity 2023.1 + sceneReferencer = GameObject.FindObjectOfType(); +#endif + + cameraObj = sceneReferencer.cameraObject.transform; + + this.enabled = true; + } + + public override void OnStopAuthority() + { + this.enabled = false; + } + + void Update() + { + if (!Application.isFocused) + return; + + if (cameraObj && characterSelection) + characterSelection.floatingInfo.forward = cameraObj.transform.forward; + + if (cameraObj && cameraTarget) + cameraObj.SetPositionAndRotation(cameraTarget.position, cameraTarget.rotation); + } + } +} diff --git a/Assets/Mirror/Examples/CharacterSelection/Scripts/SceneCamera.cs.meta b/Assets/Mirror/Examples/CharacterSelection/Scripts/SceneCamera.cs.meta new file mode 100644 index 000000000..08b9d3e2f --- /dev/null +++ b/Assets/Mirror/Examples/CharacterSelection/Scripts/SceneCamera.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: db9418a9e709a7844acac42de096cba6 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: