From c03835c06b268fe7eb2fd8769e3202f2f9006988 Mon Sep 17 00:00:00 2001 From: LTYGUY Date: Mon, 12 Feb 2024 10:29:55 +0800 Subject: [PATCH 1/3] fixed enemy teleport to current enemy when possess --- lc-hax/Scripts/Modules/Possession/PossessionMod.cs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/lc-hax/Scripts/Modules/Possession/PossessionMod.cs b/lc-hax/Scripts/Modules/Possession/PossessionMod.cs index b9874a2e..9caa4180 100644 --- a/lc-hax/Scripts/Modules/Possession/PossessionMod.cs +++ b/lc-hax/Scripts/Modules/Possession/PossessionMod.cs @@ -13,7 +13,6 @@ internal sealed class PossessionMod : MonoBehaviour { Possession Possession { get; } = new(); Coroutine? UpdateCoroutine { get; set; } = null; CharacterMovement? CharacterMovement { get; set; } = null; - KeyboardMovement? Keyboard { get; set; } = null; MousePan? MousePan { get; set; } = null; bool FirstUpdate { get; set; } = true; @@ -35,7 +34,6 @@ internal sealed class PossessionMod : MonoBehaviour { void Awake() { this.CharacterMovement = this.gameObject.AddComponent(); - this.Keyboard = this.gameObject.AddComponent(); this.MousePan = this.gameObject.AddComponent(); this.enabled = false; @@ -82,11 +80,9 @@ void ToggleNoClip() { void UpdateComponentsOnCurrentState(bool thisGameObjectIsEnabled) { if (this.MousePan is not MousePan mousePan) return; if (this.CharacterMovement is not CharacterMovement rigidbodyKeyboard) return; - if (this.Keyboard is not KeyboardMovement keyboard) return; mousePan.enabled = thisGameObjectIsEnabled; rigidbodyKeyboard.enabled = !this.NoClipEnabled; - keyboard.enabled = this.NoClipEnabled; } IEnumerator EndOfFrameCoroutine() { @@ -119,9 +115,10 @@ void EndOfFrameUpdate() { agent.updatePosition = false; agent.updateRotation = false; } - - characterMovement.Init(); + characterMovement.enabled = false; this.transform.position = enemy.transform.position; + characterMovement.enabled = true; + characterMovement.Init(); this.UpdateComponentsOnCurrentState(true); } @@ -163,8 +160,8 @@ void SetEnemyColliders(EnemyAI enemy, bool enabled) => internal void Possess(EnemyAI enemy) { this.Unpossess(); - this.Possession.SetEnemy(enemy); this.FirstUpdate = true; + this.Possession.SetEnemy(enemy); } // Releases possession of the current enemy From c5c40999f63a2ffb845a1411e5bb6ea02e57cf21 Mon Sep 17 00:00:00 2001 From: LTYGUY Date: Mon, 12 Feb 2024 11:13:09 +0800 Subject: [PATCH 2/3] Revert "fixed enemy teleport to current enemy when possess" This reverts commit c03835c06b268fe7eb2fd8769e3202f2f9006988. --- lc-hax/Scripts/Modules/Possession/PossessionMod.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/lc-hax/Scripts/Modules/Possession/PossessionMod.cs b/lc-hax/Scripts/Modules/Possession/PossessionMod.cs index 9caa4180..b9874a2e 100644 --- a/lc-hax/Scripts/Modules/Possession/PossessionMod.cs +++ b/lc-hax/Scripts/Modules/Possession/PossessionMod.cs @@ -13,6 +13,7 @@ internal sealed class PossessionMod : MonoBehaviour { Possession Possession { get; } = new(); Coroutine? UpdateCoroutine { get; set; } = null; CharacterMovement? CharacterMovement { get; set; } = null; + KeyboardMovement? Keyboard { get; set; } = null; MousePan? MousePan { get; set; } = null; bool FirstUpdate { get; set; } = true; @@ -34,6 +35,7 @@ internal sealed class PossessionMod : MonoBehaviour { void Awake() { this.CharacterMovement = this.gameObject.AddComponent(); + this.Keyboard = this.gameObject.AddComponent(); this.MousePan = this.gameObject.AddComponent(); this.enabled = false; @@ -80,9 +82,11 @@ void ToggleNoClip() { void UpdateComponentsOnCurrentState(bool thisGameObjectIsEnabled) { if (this.MousePan is not MousePan mousePan) return; if (this.CharacterMovement is not CharacterMovement rigidbodyKeyboard) return; + if (this.Keyboard is not KeyboardMovement keyboard) return; mousePan.enabled = thisGameObjectIsEnabled; rigidbodyKeyboard.enabled = !this.NoClipEnabled; + keyboard.enabled = this.NoClipEnabled; } IEnumerator EndOfFrameCoroutine() { @@ -115,10 +119,9 @@ void EndOfFrameUpdate() { agent.updatePosition = false; agent.updateRotation = false; } - characterMovement.enabled = false; - this.transform.position = enemy.transform.position; - characterMovement.enabled = true; + characterMovement.Init(); + this.transform.position = enemy.transform.position; this.UpdateComponentsOnCurrentState(true); } @@ -160,8 +163,8 @@ void SetEnemyColliders(EnemyAI enemy, bool enabled) => internal void Possess(EnemyAI enemy) { this.Unpossess(); - this.FirstUpdate = true; this.Possession.SetEnemy(enemy); + this.FirstUpdate = true; } // Releases possession of the current enemy From 39535e58965f97723b45a8bb2633e44232f5b49a Mon Sep 17 00:00:00 2001 From: LTYGUY Date: Mon, 12 Feb 2024 12:05:03 +0800 Subject: [PATCH 3/3] refactored possession to fix bugs --- .../Scripts/Components/CharacterMovement.cs | 6 +++ .../Modules/Possession/PossessionMod.cs | 49 +++++++++++++------ 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/lc-hax/Scripts/Components/CharacterMovement.cs b/lc-hax/Scripts/Components/CharacterMovement.cs index da4deb59..45a1c6ad 100644 --- a/lc-hax/Scripts/Components/CharacterMovement.cs +++ b/lc-hax/Scripts/Components/CharacterMovement.cs @@ -22,6 +22,9 @@ internal class CharacterMovement : MonoBehaviour { bool IsSprintHeld { get; set; } = false; float SprintTimer { get; set; } = 0.0f; Keyboard Keyboard { get; set; } = Keyboard.current; + KeyboardMovement? NoClipKeyboard { get; set; } = null; + + internal void SetNoClipMode(bool enabled) => this.NoClipKeyboard!.enabled = enabled; // Initialize method internal void Init() { @@ -31,6 +34,7 @@ internal void Init() { void Awake() { this.Keyboard = Keyboard.current; + this.NoClipKeyboard = this.gameObject.AddComponent(); this.CharacterController = this.gameObject.AddComponent(); this.CharacterController.height = 0.0f; // Adjust as needed this.CharacterController.center = new Vector3(0.0f, 0.3f, 0.5f); // Adjust as needed @@ -39,6 +43,8 @@ void Awake() { // Update is called once per frame void Update() { + if (this.NoClipKeyboard!.enabled) return; + // Read movement input from keyboard Vector2 moveInput = new Vector2( this.Keyboard.dKey.ReadValue() - this.Keyboard.aKey.ReadValue(), diff --git a/lc-hax/Scripts/Modules/Possession/PossessionMod.cs b/lc-hax/Scripts/Modules/Possession/PossessionMod.cs index b9874a2e..dac1742f 100644 --- a/lc-hax/Scripts/Modules/Possession/PossessionMod.cs +++ b/lc-hax/Scripts/Modules/Possession/PossessionMod.cs @@ -12,8 +12,8 @@ internal sealed class PossessionMod : MonoBehaviour { Possession Possession { get; } = new(); Coroutine? UpdateCoroutine { get; set; } = null; + GameObject? CharacterMovementInstance { get; set; } = null; CharacterMovement? CharacterMovement { get; set; } = null; - KeyboardMovement? Keyboard { get; set; } = null; MousePan? MousePan { get; set; } = null; bool FirstUpdate { get; set; } = true; @@ -34,14 +34,27 @@ internal sealed class PossessionMod : MonoBehaviour { }; void Awake() { - this.CharacterMovement = this.gameObject.AddComponent(); - this.Keyboard = this.gameObject.AddComponent(); + this.InitCharacterMovement(); this.MousePan = this.gameObject.AddComponent(); this.enabled = false; PossessionMod.Instance = this; } + void InitCharacterMovement(Vector3 initPos = default) { + this.CharacterMovementInstance = new GameObject("Hax_CharacterMovement"); + this.CharacterMovementInstance.transform.position = initPos; + this.CharacterMovement = this.CharacterMovementInstance.AddComponent(); + this.CharacterMovement.Init(); + DontDestroyOnLoad(this.CharacterMovementInstance); + } + + void UnInitCharacterMovement() { + if (this.CharacterMovementInstance is not GameObject characterMovementInstance) return; + + Destroy(characterMovementInstance); + } + void OnEnable() { InputListener.OnNPress += this.ToggleNoClip; InputListener.OnZPress += this.Unpossess; @@ -81,12 +94,11 @@ void ToggleNoClip() { void UpdateComponentsOnCurrentState(bool thisGameObjectIsEnabled) { if (this.MousePan is not MousePan mousePan) return; - if (this.CharacterMovement is not CharacterMovement rigidbodyKeyboard) return; - if (this.Keyboard is not KeyboardMovement keyboard) return; + if (this.CharacterMovement is not CharacterMovement characterMovement) return; mousePan.enabled = thisGameObjectIsEnabled; - rigidbodyKeyboard.enabled = !this.NoClipEnabled; - keyboard.enabled = this.NoClipEnabled; + characterMovement.gameObject.SetActive(thisGameObjectIsEnabled); + characterMovement.SetNoClipMode(this.NoClipEnabled); } IEnumerator EndOfFrameCoroutine() { @@ -120,19 +132,18 @@ void EndOfFrameUpdate() { agent.updateRotation = false; } - characterMovement.Init(); - this.transform.position = enemy.transform.position; + this.InitCharacterMovement(enemy.transform.position); this.UpdateComponentsOnCurrentState(true); } if (!this.EnemyControllers.TryGetValue(enemy.GetType(), out IController controller)) { - this.UpdateEnemyPositionToHere(enemy); + this.UpdateEnemyPosition(enemy); this.UpdateCameraPosition(camera, enemy); return; } else if (controller.IsAbleToMove(enemy)) { - this.UpdateEnemyPositionToHere(enemy); + this.UpdateEnemyPosition(enemy); this.HandleEnemyMovements(controller, enemy, characterMovement.IsMoving, characterMovement.IsSprinting); this.EnemyUpdate(controller, enemy); } @@ -142,17 +153,22 @@ void EndOfFrameUpdate() { } void UpdateCameraPosition(Camera camera, EnemyAI enemy) { - camera.transform.position = this.transform.position + (3.0f * (Vector3.up - enemy.transform.forward)); + if (this.CharacterMovement is not CharacterMovement characterMovement) return; + + camera.transform.position = characterMovement.transform.position + (3.0f * (Vector3.up - enemy.transform.forward)); camera.transform.rotation = this.transform.rotation; + characterMovement.transform.rotation = this.transform.rotation; } // Updates enemy's position to match the possessed object's position - void UpdateEnemyPositionToHere(EnemyAI enemy) { + void UpdateEnemyPosition(EnemyAI enemy) { + if (this.CharacterMovement is not CharacterMovement characterMovement) return; + enemy.updatePositionThreshold = 0; Vector3 enemyEuler = enemy.transform.eulerAngles; enemyEuler.y = this.transform.eulerAngles.y; enemy.transform.eulerAngles = enemyEuler; - enemy.transform.position = this.transform.position; + enemy.transform.position = characterMovement.transform.position; } // Disables/enables colliders of the possessed enemy @@ -163,18 +179,19 @@ void SetEnemyColliders(EnemyAI enemy, bool enabled) => internal void Possess(EnemyAI enemy) { this.Unpossess(); - this.Possession.SetEnemy(enemy); this.FirstUpdate = true; + this.Possession.SetEnemy(enemy); } // Releases possession of the current enemy internal void Unpossess() { + this.UnInitCharacterMovement(); if (this.Possession.Enemy is not EnemyAI enemy) return; if (enemy.agent.Unfake() is NavMeshAgent navMeshAgent) { navMeshAgent.updatePosition = true; navMeshAgent.updateRotation = true; - this.UpdateEnemyPositionToHere(enemy); + this.UpdateEnemyPosition(enemy); _ = enemy.agent.Warp(enemy.transform.position); }