fix(CharacterSelection Example): Separate SceneCamera script (#3888)

* CharSelection: Separate SceneCamera script

* SceneCamera: SetPositionAndRotation

* Code Cleanup
This commit is contained in:
MrGadget 2024-08-10 14:43:29 -04:00 committed by GitHub
parent 6f28f716d5
commit 9064dab293
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 132 additions and 30 deletions

View File

@ -1404,6 +1404,7 @@ GameObject:
- component: {fileID: 3603318197913287424} - component: {fileID: 3603318197913287424}
- component: {fileID: -5286820321314269966} - component: {fileID: -5286820321314269966}
- component: {fileID: 108717044788610704} - component: {fileID: 108717044788610704}
- component: {fileID: 8165614180483664709}
m_Layer: 0 m_Layer: 0
m_Name: CharacterAssault m_Name: CharacterAssault
m_TagString: Untagged m_TagString: Untagged
@ -1541,7 +1542,6 @@ MonoBehaviour:
animRotation: 0 animRotation: 0
velocity: {x: 0, y: 0, z: 0} velocity: {x: 0, y: 0, z: 0}
direction: {x: 0, y: 0, z: 0} direction: {x: 0, y: 0, z: 0}
cameraTarget: {fileID: 8318204445083151948}
--- !u!114 &-5286820321314269966 --- !u!114 &-5286820321314269966
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -1588,6 +1588,25 @@ MonoBehaviour:
- {fileID: 3074504445760471458} - {fileID: 3074504445760471458}
- {fileID: 3074504446469250245} - {fileID: 3074504446469250245}
- {fileID: 3074504445848950737} - {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 --- !u!1 &3074504447184887503
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@ -343,6 +343,7 @@ GameObject:
- component: {fileID: -7643125540303550518} - component: {fileID: -7643125540303550518}
- component: {fileID: -7784293741018888422} - component: {fileID: -7784293741018888422}
- component: {fileID: 597190161414468650} - component: {fileID: 597190161414468650}
- component: {fileID: -6486408998751470869}
m_Layer: 0 m_Layer: 0
m_Name: CharacterHeavy m_Name: CharacterHeavy
m_TagString: Untagged m_TagString: Untagged
@ -480,7 +481,6 @@ MonoBehaviour:
animRotation: 0 animRotation: 0
velocity: {x: 0, y: 0, z: 0} velocity: {x: 0, y: 0, z: 0}
direction: {x: 0, y: 0, z: 0} direction: {x: 0, y: 0, z: 0}
cameraTarget: {fileID: 5721588646566991880}
--- !u!114 &-7784293741018888422 --- !u!114 &-7784293741018888422
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -524,6 +524,25 @@ MonoBehaviour:
- {fileID: 2320049527593028056} - {fileID: 2320049527593028056}
- {fileID: 2320049526879068635} - {fileID: 2320049526879068635}
- {fileID: 2320049528211028347} - {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 --- !u!1 &2320049527168386026
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@ -778,6 +778,7 @@ GameObject:
- component: {fileID: -4474909813393930685} - component: {fileID: -4474909813393930685}
- component: {fileID: 3540726253812090381} - component: {fileID: 3540726253812090381}
- component: {fileID: 4511077071076738942} - component: {fileID: 4511077071076738942}
- component: {fileID: 958723225240916811}
m_Layer: 0 m_Layer: 0
m_Name: CharacterMedic m_Name: CharacterMedic
m_TagString: Untagged m_TagString: Untagged
@ -915,7 +916,6 @@ MonoBehaviour:
animRotation: 0 animRotation: 0
velocity: {x: 0, y: 0, z: 0} velocity: {x: 0, y: 0, z: 0}
direction: {x: 0, y: 0, z: 0} direction: {x: 0, y: 0, z: 0}
cameraTarget: {fileID: 2977242866262118958}
--- !u!114 &3540726253812090381 --- !u!114 &3540726253812090381
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -958,6 +958,25 @@ MonoBehaviour:
- {fileID: 9105303183086384159} - {fileID: 9105303183086384159}
- {fileID: 9105303183046110706} - {fileID: 9105303183046110706}
- {fileID: 9105303183986044559} - {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 --- !u!1 &9105303183937996607
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@ -55,18 +55,10 @@ public enum GroundState : byte { Jumping, Falling, Grounded }
[ReadOnly, SerializeField] Vector3Int velocity; [ReadOnly, SerializeField] Vector3Int velocity;
[ReadOnly, SerializeField] Vector3 direction; [ReadOnly, SerializeField] Vector3 direction;
private Transform cameraObj;
public Transform cameraTarget;
private SceneReferencer sceneReferencer;
private CharacterSelection characterSelection;
public void Awake() public void Awake()
{ {
if (characterController == null) if (characterController == null)
characterController = GetComponent<CharacterController>(); characterController = GetComponent<CharacterController>();
if (characterSelection == null)
characterSelection = GetComponent<CharacterSelection>();
// Override CharacterController default values // Override CharacterController default values
characterController.enabled = false; characterController.enabled = false;
@ -82,14 +74,6 @@ public override void OnStartAuthority()
{ {
characterController.enabled = true; characterController.enabled = true;
this.enabled = true; this.enabled = true;
#if UNITY_2022_2_OR_NEWER
sceneReferencer = GameObject.FindAnyObjectByType<SceneReferencer>();
#else
// Deprecated in Unity 2023.1
sceneReferencer = GameObject.FindObjectOfType<SceneReferencer>();
#endif
cameraObj = sceneReferencer.cameraObject.transform;
} }
public override void OnStopAuthority() public override void OnStopAuthority()
@ -100,12 +84,9 @@ public override void OnStopAuthority()
void Update() void Update()
{ {
if (cameraObj && characterSelection) if (!Application.isFocused)
{ return;
characterSelection.floatingInfo.forward = cameraObj.transform.forward;
}
if (!Application.isFocused) return;
if (!characterController.enabled) if (!characterController.enabled)
return; return;
@ -121,12 +102,6 @@ void Update()
// Diagnostic velocity...FloorToInt for display purposes // Diagnostic velocity...FloorToInt for display purposes
velocity = Vector3Int.FloorToInt(characterController.velocity); velocity = Vector3Int.FloorToInt(characterController.velocity);
if (cameraObj != null)
{
cameraObj.position = cameraTarget.position;
cameraObj.rotation = cameraTarget.rotation;
}
} }
// TODO: Turning works while airborne...feature? // TODO: Turning works while airborne...feature?

View File

@ -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<CharacterSelection>();
cameraTarget = transform.Find("CameraTarget");
this.enabled = false;
}
public override void OnStartAuthority()
{
#if UNITY_2022_2_OR_NEWER
sceneReferencer = GameObject.FindAnyObjectByType<SceneReferencer>();
#else
// Deprecated in Unity 2023.1
sceneReferencer = GameObject.FindObjectOfType<SceneReferencer>();
#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);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: db9418a9e709a7844acac42de096cba6
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: