From d9629ec55504eff93ad24209a1a86cf2b45626d5 Mon Sep 17 00:00:00 2001 From: Melbyj1125 <60886701+Melbyj1125@users.noreply.github.com> Date: Wed, 23 Feb 2022 13:55:30 -0600 Subject: [PATCH] lose Mom on wall and kick renetworked --- .../DebugSlide/dMoveCrouchState.cs | 5 +-- .../DebugSlide/dMoveSlideState.cs | 5 +-- .../DebugMoveState/dMoveStateManager.cs | 32 +++++++++++++++-- .../MovementState/MoveStateManager.cs | 29 +++++++++++++-- .../MovementState/Slide/MoveCrouchState.cs | 5 +-- .../MovementState/Slide/MoveSlideState.cs | 5 +-- .../OffenseState/OffenseStateManager.cs | 35 +++++++++++++++---- UserSettings/EditorUserSettings.asset | 6 ++-- 8 files changed, 92 insertions(+), 30 deletions(-) diff --git a/Assets/Scripts/PlayerScripts/DebugStateMachine/DebugMoveState/DebugSlide/dMoveCrouchState.cs b/Assets/Scripts/PlayerScripts/DebugStateMachine/DebugMoveState/DebugSlide/dMoveCrouchState.cs index bdb153b..ea3ad2b 100644 --- a/Assets/Scripts/PlayerScripts/DebugStateMachine/DebugMoveState/DebugSlide/dMoveCrouchState.cs +++ b/Assets/Scripts/PlayerScripts/DebugStateMachine/DebugMoveState/DebugSlide/dMoveCrouchState.cs @@ -7,7 +7,6 @@ public class dMoveCrouchState : dMoveBaseState //Slide Variables RaycastHit slideRay; // slide raycast - int layerMask; public override void EnterState(dMoveStateManager mSM, dMoveBaseState previousState){ @@ -17,8 +16,6 @@ public class dMoveCrouchState : dMoveBaseState mSM.moveController.height *= .5f; mSM.moveController.center = new Vector3(0,mSM.moveController.center.y - mSM.moveController.height * .5f,0); //mSM.moveController.Move(new Vector3(0,-mSM.moveController.height * .5f,0)); - layerMask = 1 << 3; - layerMask = ~layerMask; } } @@ -39,7 +36,7 @@ public class dMoveCrouchState : dMoveBaseState //If player isn't pressing either Q or the joystick button they stop crouching if nothing is above them if((!Input.GetKey(KeyCode.JoystickButton1) && !Input.GetKey(KeyCode.Q))){ - if ((Physics.Raycast(mSM.gameObject.transform.position + new Vector3(0,1f,0), Vector3.up, out slideRay, 2f, layerMask) == false)){ + if ((Physics.Raycast(mSM.gameObject.transform.position + new Vector3(0,1f,0), Vector3.up, out slideRay, 2f, mSM.layerMask) == false)){ mSM.SwitchState(mSM.IdleState); } diff --git a/Assets/Scripts/PlayerScripts/DebugStateMachine/DebugMoveState/DebugSlide/dMoveSlideState.cs b/Assets/Scripts/PlayerScripts/DebugStateMachine/DebugMoveState/DebugSlide/dMoveSlideState.cs index 9a08b84..f0494a1 100644 --- a/Assets/Scripts/PlayerScripts/DebugStateMachine/DebugMoveState/DebugSlide/dMoveSlideState.cs +++ b/Assets/Scripts/PlayerScripts/DebugStateMachine/DebugMoveState/DebugSlide/dMoveSlideState.cs @@ -7,7 +7,6 @@ public class dMoveSlideState : dMoveBaseState //Slide Variables float originalTraction; // Traction before slide started RaycastHit slideRay; // slide raycast - int layerMask; public override void EnterState(dMoveStateManager mSM, dMoveBaseState previousState){ @@ -18,8 +17,6 @@ public class dMoveSlideState : dMoveBaseState mSM.pStats.Traction = 0.01f; mSM.moveController.center = new Vector3(0,mSM.moveController.center.y - mSM.moveController.height * .5f,0); //mSM.moveController.Move(new Vector3(0,-0.1f,0)); - layerMask = 1 << 3; - layerMask = ~layerMask; } public override void ExitState(dMoveStateManager mSM, dMoveBaseState nextState){ @@ -51,7 +48,7 @@ public class dMoveSlideState : dMoveBaseState //If player isn't pressing either Q or the joystick button they stop sliding if nothing is above them if((!Input.GetKey(KeyCode.JoystickButton1) && !Input.GetKey(KeyCode.Q))){ - if ((Physics.Raycast(mSM.gameObject.transform.position + new Vector3(0,1f,0), Vector3.up, out slideRay, 2f, layerMask) == false)){ + if ((Physics.Raycast(mSM.gameObject.transform.position + new Vector3(0,1f,0), Vector3.up, out slideRay, 2f, mSM.layerMask) == false)){ //Determine which state to go into based on player speed if(mSM.calculatedCurVel < mSM.walkLimit){ diff --git a/Assets/Scripts/PlayerScripts/DebugStateMachine/DebugMoveState/dMoveStateManager.cs b/Assets/Scripts/PlayerScripts/DebugStateMachine/DebugMoveState/dMoveStateManager.cs index 615ea8f..acd9b8d 100644 --- a/Assets/Scripts/PlayerScripts/DebugStateMachine/DebugMoveState/dMoveStateManager.cs +++ b/Assets/Scripts/PlayerScripts/DebugStateMachine/DebugMoveState/dMoveStateManager.cs @@ -59,6 +59,10 @@ public class dMoveStateManager : NetworkBehaviour private Vector3 moveX; // Local Vertical Vector public Vector3 driftVel; // Lerped Movement Vector public float calculatedCurVel; // calculated current vel using driftVel + public int layerMask; // LayerMask to exclude player + RaycastHit wallHitTop, wallHitBot; // Raycast for momentum loss on wall hit + private Vector3 lastVel; // previous vel + private bool firstWallHit = false; // hit the wall for the first time //Camera Variables private Vector3 camRotation; // cameras camera rotation vector @@ -90,6 +94,12 @@ public class dMoveStateManager : NetworkBehaviour pStats = GetComponent(); // set PlayerStats aSM = GetComponent(); // aerial state manager //// + + ////Initialize Variables + layerMask = 1 << 3; + layerMask = ~layerMask; + //// + } // Start is called before the first frame update @@ -102,6 +112,7 @@ public class dMoveStateManager : NetworkBehaviour //Slide Upwards Variable distToGround = GetComponent().bounds.extents.y; // set players distance to ground + lastVel = Vector3.zero; //if (!IsLocalPlayer) { return; } Cursor.lockState = CursorLockMode.Locked; // Lock cursor on start if you are the local player @@ -117,7 +128,7 @@ public class dMoveStateManager : NetworkBehaviour //if grappling in aerial state manager swap to grapple here if(currentState != GrappleAirState){ - if(aSM.currentState == aSM.GrappleAirState && (currentState != SlideState && currentState != RagdollState && currentState != RecoveringState)){ + if(aSM.currentState == aSM.GrappleAirState && (currentState != SlideState && currentState != CrouchState && currentState != RagdollState && currentState != RecoveringState)){ SwitchState(GrappleAirState); } } @@ -161,7 +172,7 @@ public class dMoveStateManager : NetworkBehaviour return pStats.CurVel; } //If current speed is below min when pressed set to minimum speed - else if (pStats.CurVel < pStats.MinVel) + else if (pStats.CurVel < pStats.MinVel || firstWallHit == true) { pStats.CurVel = pStats.MinVel; return pStats.MinVel; @@ -192,9 +203,24 @@ public class dMoveStateManager : NetworkBehaviour //Checks if movement keys have been pressed and calculates correct vector moveX = transform.right * Input.GetAxis("Horizontal") * Time.deltaTime * PlayerSpeed(); moveZ = transform.forward * Input.GetAxis("Vertical") * Time.deltaTime * PlayerSpeed(); - vel = moveX + moveZ; Vector3 moveXZ = new Vector3(vel.x, 0, vel.z); + + //Raycast offset + Vector3 rayOffset = moveXZ - lastVel; + + //Check if wall is in direction player is moving + if (((Physics.Raycast(gameObject.transform.position + new Vector3(0,.4f,0) + rayOffset, moveXZ.normalized, out wallHitBot, .2f, layerMask) == true) || ((currentState != SlideState || currentState != CrouchState) && (Physics.Raycast(gameObject.transform.position + new Vector3(0,2.2f,0), moveXZ.normalized, out wallHitTop, .5f, layerMask) == true))) && !firstWallHit){ + CancelMomentum(); + firstWallHit = true; + } + else if((Physics.Raycast(gameObject.transform.position + new Vector3(0,.4f,0) + rayOffset, moveXZ.normalized, out wallHitBot, .2f, layerMask) == false) || ((currentState != SlideState || currentState != CrouchState) && (Physics.Raycast(gameObject.transform.position + new Vector3(0,2.2f,0), moveXZ.normalized, out wallHitTop, .5f, layerMask) == false))){ + firstWallHit = false; + } + + Debug.DrawRay(gameObject.transform.position + new Vector3(0,.4f,0) + rayOffset, moveXZ.normalized * 1f, Color.red); + Debug.DrawRay(gameObject.transform.position + new Vector3(0,2.2f,0) + rayOffset, moveXZ.normalized * 1f, Color.red); + driftVel = Vector3.Lerp(driftVel, moveXZ, pStats.Traction * Time.deltaTime); if(currentState == GrappleAirState){ driftVel = Vector3.zero; diff --git a/Assets/Scripts/PlayerScripts/NetworkedStateMachines/MovementState/MoveStateManager.cs b/Assets/Scripts/PlayerScripts/NetworkedStateMachines/MovementState/MoveStateManager.cs index c2eed67..7901a79 100644 --- a/Assets/Scripts/PlayerScripts/NetworkedStateMachines/MovementState/MoveStateManager.cs +++ b/Assets/Scripts/PlayerScripts/NetworkedStateMachines/MovementState/MoveStateManager.cs @@ -59,6 +59,10 @@ public class MoveStateManager : NetworkBehaviour private Vector3 moveX; // Local Vertical Vector public Vector3 driftVel; // Lerped Movement Vector public float calculatedCurVel; // calculated current vel using driftVel + public int layerMask; // LayerMask to exclude player + RaycastHit wallHitTop, wallHitBot; // Raycast for momentum loss on wall hit + private Vector3 lastVel; // previous vel + private bool firstWallHit = false; // hit the wall for the first time //Camera Variables private Vector3 camRotation; // cameras camera rotation vector @@ -90,6 +94,12 @@ public class MoveStateManager : NetworkBehaviour pStats = GetComponent(); // set PlayerStats aSM = GetComponent(); // aerial state manager //// + + ////Initialize Variables + layerMask = 1 << 3; + layerMask = ~layerMask; + //// + } // Start is called before the first frame update @@ -160,7 +170,7 @@ public class MoveStateManager : NetworkBehaviour return pStats.CurVel; } //If current speed is below min when pressed set to minimum speed - else if (pStats.CurVel < pStats.MinVel) + else if (pStats.CurVel < pStats.MinVel || firstWallHit == true) { pStats.CurVel = pStats.MinVel; return pStats.MinVel; @@ -191,9 +201,24 @@ public class MoveStateManager : NetworkBehaviour //Checks if movement keys have been pressed and calculates correct vector moveX = transform.right * Input.GetAxis("Horizontal") * Time.deltaTime * PlayerSpeed(); moveZ = transform.forward * Input.GetAxis("Vertical") * Time.deltaTime * PlayerSpeed(); - vel = moveX + moveZ; Vector3 moveXZ = new Vector3(vel.x, 0, vel.z); + + //Raycast offset + Vector3 rayOffset = moveXZ - lastVel; + + //Check if wall is in direction player is moving + if (((Physics.Raycast(gameObject.transform.position + new Vector3(0,.4f,0) + rayOffset, moveXZ.normalized, out wallHitBot, .2f, layerMask) == true) || ((currentState != SlideState || currentState != CrouchState) && (Physics.Raycast(gameObject.transform.position + new Vector3(0,2.2f,0), moveXZ.normalized, out wallHitTop, .5f, layerMask) == true))) && !firstWallHit){ + CancelMomentum(); + firstWallHit = true; + } + else if((Physics.Raycast(gameObject.transform.position + new Vector3(0,.4f,0) + rayOffset, moveXZ.normalized, out wallHitBot, .2f, layerMask) == false) || ((currentState != SlideState || currentState != CrouchState) && (Physics.Raycast(gameObject.transform.position + new Vector3(0,2.2f,0), moveXZ.normalized, out wallHitTop, .5f, layerMask) == false))){ + firstWallHit = false; + } + + Debug.DrawRay(gameObject.transform.position + new Vector3(0,.4f,0) + rayOffset, moveXZ.normalized * 1f, Color.red); + Debug.DrawRay(gameObject.transform.position + new Vector3(0,2.2f,0) + rayOffset, moveXZ.normalized * 1f, Color.red); + driftVel = Vector3.Lerp(driftVel, moveXZ, pStats.Traction * Time.deltaTime); if(currentState == GrappleAirState){ driftVel = Vector3.zero; diff --git a/Assets/Scripts/PlayerScripts/NetworkedStateMachines/MovementState/Slide/MoveCrouchState.cs b/Assets/Scripts/PlayerScripts/NetworkedStateMachines/MovementState/Slide/MoveCrouchState.cs index 14cadfa..83185f9 100644 --- a/Assets/Scripts/PlayerScripts/NetworkedStateMachines/MovementState/Slide/MoveCrouchState.cs +++ b/Assets/Scripts/PlayerScripts/NetworkedStateMachines/MovementState/Slide/MoveCrouchState.cs @@ -7,7 +7,6 @@ public class MoveCrouchState : MoveBaseState //Slide Variables RaycastHit slideRay; // slide raycast - int layerMask; public override void EnterState(MoveStateManager mSM, MoveBaseState previousState){ @@ -17,8 +16,6 @@ public class MoveCrouchState : MoveBaseState mSM.moveController.height *= .5f; mSM.moveController.center = new Vector3(0,mSM.moveController.center.y - mSM.moveController.height * .5f,0); //mSM.moveController.Move(new Vector3(0,-mSM.moveController.height * .5f,0)); - layerMask = 1 << 3; - layerMask = ~layerMask; } } @@ -39,7 +36,7 @@ public class MoveCrouchState : MoveBaseState //If player isn't pressing either Q or the joystick button they stop crouching if nothing is above them if((!Input.GetKey(KeyCode.JoystickButton1) && !Input.GetKey(KeyCode.Q))){ - if ((Physics.Raycast(mSM.gameObject.transform.position + new Vector3(0,1f,0), Vector3.up, out slideRay, 2f, layerMask) == false)){ + if ((Physics.Raycast(mSM.gameObject.transform.position + new Vector3(0,1f,0), Vector3.up, out slideRay, 2f, mSM.layerMask) == false)){ mSM.SwitchState(mSM.IdleState); } diff --git a/Assets/Scripts/PlayerScripts/NetworkedStateMachines/MovementState/Slide/MoveSlideState.cs b/Assets/Scripts/PlayerScripts/NetworkedStateMachines/MovementState/Slide/MoveSlideState.cs index c688438..df9d282 100644 --- a/Assets/Scripts/PlayerScripts/NetworkedStateMachines/MovementState/Slide/MoveSlideState.cs +++ b/Assets/Scripts/PlayerScripts/NetworkedStateMachines/MovementState/Slide/MoveSlideState.cs @@ -7,7 +7,6 @@ public class MoveSlideState : MoveBaseState //Slide Variables float originalTraction; // Traction before slide started RaycastHit slideRay; // slide raycast - int layerMask; public override void EnterState(MoveStateManager mSM, MoveBaseState previousState){ @@ -18,8 +17,6 @@ public class MoveSlideState : MoveBaseState mSM.pStats.Traction = 0.01f; mSM.moveController.center = new Vector3(0,mSM.moveController.center.y - mSM.moveController.height * .5f,0); //mSM.moveController.Move(new Vector3(0,-0.1f,0)); - layerMask = 1 << 3; - layerMask = ~layerMask; } public override void ExitState(MoveStateManager mSM, MoveBaseState nextState){ @@ -51,7 +48,7 @@ public class MoveSlideState : MoveBaseState //If player isn't pressing either Q or the joystick button they stop sliding if nothing is above them if((!Input.GetKey(KeyCode.JoystickButton1) && !Input.GetKey(KeyCode.Q))){ - if ((Physics.Raycast(mSM.gameObject.transform.position + new Vector3(0,1f,0), Vector3.up, out slideRay, 2f, layerMask) == false)){ + if ((Physics.Raycast(mSM.gameObject.transform.position + new Vector3(0,1f,0), Vector3.up, out slideRay, 2f, mSM.layerMask) == false)){ //Determine which state to go into based on player speed if(mSM.calculatedCurVel < mSM.walkLimit){ diff --git a/Assets/Scripts/PlayerScripts/NetworkedStateMachines/OffenseState/OffenseStateManager.cs b/Assets/Scripts/PlayerScripts/NetworkedStateMachines/OffenseState/OffenseStateManager.cs index 5c41cf8..c5df95a 100644 --- a/Assets/Scripts/PlayerScripts/NetworkedStateMachines/OffenseState/OffenseStateManager.cs +++ b/Assets/Scripts/PlayerScripts/NetworkedStateMachines/OffenseState/OffenseStateManager.cs @@ -2,6 +2,7 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; using MLAPI; +using MLAPI.Messaging; public class OffenseStateManager : NetworkBehaviour { @@ -104,16 +105,38 @@ public class OffenseStateManager : NetworkBehaviour { if (!IsLocalPlayer) { return; } Collider myCollider = collision.contacts[0].thisCollider; - if (collision.transform.CompareTag("kickable") && myCollider == legHitbox.GetComponent()){ - if(collision.gameObject.GetComponent().isKinematic == true){ - collision.gameObject.GetComponent().isKinematic = false; - } + + // Kickable items must be handled through the server since they need to modify the NetworkTransform + if (collision.transform.CompareTag("kickable") && myCollider == legHitbox.GetComponent()) { Vector3 direction = this.transform.forward; - Debug.Log(direction); - collision.rigidbody.AddForce(direction * pStats.KickPow, ForceMode.Impulse); + //ulong prefabHash = collision.gameObject.GetComponent().PrefabHash; + ulong netObjID = collision.gameObject.GetComponent().NetworkObjectId; + ApplyKickServerRPC(direction, netObjID); } + if (collision.transform.CompareTag("destroyable") && myCollider == legHitbox.GetComponent()){ collision.transform.gameObject.GetComponent().damage(pStats.KickPow); } } + + [ServerRpc(RequireOwnership = false)] + private void ApplyKickServerRPC(Vector3 direction, ulong netObjId) { + GameObject[] kickables = GameObject.FindGameObjectsWithTag("kickable"); + + foreach (GameObject kickedItem in kickables) { + // First check to make sure this is the item we kicked + if (kickedItem.GetComponent() != null && kickedItem.GetComponent().NetworkObjectId == netObjId) { + // First turn off kinematic + if (kickedItem.gameObject.GetComponent().isKinematic == true) { + kickedItem.gameObject.GetComponent().isKinematic = false; + } + + // Then apply the force + kickedItem.gameObject.GetComponent().AddForce(direction * pStats.KickPow, ForceMode.Impulse); + + // Then return since we only kicked one item and don't need to check the remainder of the items + return; + } + } + } } diff --git a/UserSettings/EditorUserSettings.asset b/UserSettings/EditorUserSettings.asset index 1e1f3fa..0fbbe09 100644 --- a/UserSettings/EditorUserSettings.asset +++ b/UserSettings/EditorUserSettings.asset @@ -24,16 +24,16 @@ EditorUserSettings: value: 22424703114646680e0b0227036c78111b125507233d28242c20137df7ee3d2cfb flags: 0 RecentlyUsedScenePath-6: - value: 22424703114646680e0b0227036c6f02131b172b282d347e38271427fb + value: 22424703114646680e0b0227036c7b151b180b652b3a27292f260573f1e33136e7a923e7ee2e26 flags: 0 RecentlyUsedScenePath-7: value: 22424703114646680e0b0227036c7b151b180b652b3a27292f260573f1e33136e7a923e7ee2e26 flags: 0 RecentlyUsedScenePath-8: - value: 22424703114646680e0b0227036c7b151b180b6501273035202c1327d1e33136e7a923e7ee2e26 + value: 22424703114646680e0b0227036c6f02131b172b282d347e38271427fb flags: 0 RecentlyUsedScenePath-9: - value: 22424703114646680e0b0227036c7b151b180b652b3a27292f260573f1e33136e7a923e7ee2e26 + value: 22424703114646680e0b0227036c7b151b180b6501273035202c1327d1e33136e7a923e7ee2e26 flags: 0 vcSharedLogLevel: value: 0d5e400f0650