From 352e22225f8a72d50f312a74591422589ab24847 Mon Sep 17 00:00:00 2001 From: juliuse98 Date: Sun, 7 Nov 2021 16:29:21 +0100 Subject: [PATCH] Basic Recoil --- .../M9/M9 Animation Controller.controller | 98 ++++++++++++++++++- .../Player/Player Animation Test.prefab | 9 +- Assets/Scripts/Player/PlayerController.cs | 6 +- Assets/Scripts/Weapons/Shoot.cs | 6 +- Assets/Scripts/Weapons/ShootAnimation.cs | 51 ++++++++-- .../Layouts/CurrentMaximizeLayout.dwlt | 66 ++++++------- 6 files changed, 186 insertions(+), 50 deletions(-) diff --git a/Assets/Objects/Weapons/M9/M9 Animation Controller.controller b/Assets/Objects/Weapons/M9/M9 Animation Controller.controller index 3ee8730..1a67fd3 100644 --- a/Assets/Objects/Weapons/M9/M9 Animation Controller.controller +++ b/Assets/Objects/Weapons/M9/M9 Animation Controller.controller @@ -7,10 +7,11 @@ AnimatorState: m_CorrespondingSourceObject: {fileID: 0} m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} - m_Name: New State + m_Name: Idle m_Speed: 1 m_CycleOffset: 0 - m_Transitions: [] + m_Transitions: + - {fileID: 1172386202742331217} m_StateMachineBehaviours: [] m_Position: {x: 50, y: 50, z: 0} m_IKOnFeet: 0 @@ -20,12 +21,39 @@ AnimatorState: m_MirrorParameterActive: 0 m_CycleOffsetParameterActive: 0 m_TimeParameterActive: 0 - m_Motion: {fileID: 4781951162920444690, guid: d1ac7df45c0d7e84993ff57afe3c4d6f, type: 3} + m_Motion: {fileID: -3100369314251171874, guid: d1ac7df45c0d7e84993ff57afe3c4d6f, type: 3} m_Tag: m_SpeedParameter: m_MirrorParameter: m_CycleOffsetParameter: m_TimeParameter: +--- !u!1102 &-885011302923738855 +AnimatorState: + serializedVersion: 6 + m_ObjectHideFlags: 1 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: Shoot + m_Speed: 1 + m_CycleOffset: 0 + m_Transitions: + - {fileID: 6832378349465690128} + m_StateMachineBehaviours: [] + m_Position: {x: 50, y: 50, z: 0} + m_IKOnFeet: 0 + m_WriteDefaultValues: 1 + m_Mirror: 0 + m_SpeedParameterActive: 1 + m_MirrorParameterActive: 0 + m_CycleOffsetParameterActive: 0 + m_TimeParameterActive: 0 + m_Motion: {fileID: 4781951162920444690, guid: d1ac7df45c0d7e84993ff57afe3c4d6f, type: 3} + m_Tag: + m_SpeedParameter: ShootSpeed + m_MirrorParameter: + m_CycleOffsetParameter: + m_TimeParameter: --- !u!91 &9100000 AnimatorController: m_ObjectHideFlags: 0 @@ -34,7 +62,19 @@ AnimatorController: m_PrefabAsset: {fileID: 0} m_Name: M9 Animation Controller serializedVersion: 5 - m_AnimatorParameters: [] + m_AnimatorParameters: + - m_Name: Shoot + m_Type: 4 + m_DefaultFloat: 0 + m_DefaultInt: 0 + m_DefaultBool: 0 + m_Controller: {fileID: 0} + - m_Name: ShootSpeed + m_Type: 1 + m_DefaultFloat: 1 + m_DefaultInt: 0 + m_DefaultBool: 0 + m_Controller: {fileID: 0} m_AnimatorLayers: - serializedVersion: 5 m_Name: Base Layer @@ -48,6 +88,53 @@ AnimatorController: m_IKPass: 0 m_SyncedLayerAffectsTiming: 0 m_Controller: {fileID: 9100000} +--- !u!1101 &1172386202742331217 +AnimatorStateTransition: + m_ObjectHideFlags: 1 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: + m_Conditions: + - m_ConditionMode: 1 + m_ConditionEvent: Shoot + m_EventTreshold: 0 + m_DstStateMachine: {fileID: 0} + m_DstState: {fileID: -885011302923738855} + m_Solo: 0 + m_Mute: 0 + m_IsExit: 0 + serializedVersion: 3 + m_TransitionDuration: 0.25 + m_TransitionOffset: 0 + m_ExitTime: 0.6 + m_HasExitTime: 0 + m_HasFixedDuration: 1 + m_InterruptionSource: 0 + m_OrderedInterruption: 1 + m_CanTransitionToSelf: 1 +--- !u!1101 &6832378349465690128 +AnimatorStateTransition: + m_ObjectHideFlags: 1 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_Name: + m_Conditions: [] + m_DstStateMachine: {fileID: 0} + m_DstState: {fileID: -4965080280457402183} + m_Solo: 0 + m_Mute: 0 + m_IsExit: 0 + serializedVersion: 3 + m_TransitionDuration: 0.1 + m_TransitionOffset: 0 + m_ExitTime: 0.9 + m_HasExitTime: 1 + m_HasFixedDuration: 1 + m_InterruptionSource: 0 + m_OrderedInterruption: 1 + m_CanTransitionToSelf: 1 --- !u!1107 &7614406280852874800 AnimatorStateMachine: serializedVersion: 6 @@ -60,6 +147,9 @@ AnimatorStateMachine: - serializedVersion: 1 m_State: {fileID: -4965080280457402183} m_Position: {x: 300, y: 120, z: 0} + - serializedVersion: 1 + m_State: {fileID: -885011302923738855} + m_Position: {x: 310, y: 230, z: 0} m_ChildStateMachines: [] m_AnyStateTransitions: [] m_EntryTransitions: [] diff --git a/Assets/Prefabs/Player/Player Animation Test.prefab b/Assets/Prefabs/Player/Player Animation Test.prefab index e4f0b95..a3fcb0b 100644 --- a/Assets/Prefabs/Player/Player Animation Test.prefab +++ b/Assets/Prefabs/Player/Player Animation Test.prefab @@ -688,6 +688,7 @@ MonoBehaviour: muzzle: {fileID: 3649358604728444914} shootAnim: {fileID: 4276885029697240453} fireRate: 320 + gunHoldPos: {fileID: 3682030003239559187} --- !u!114 &6207632454083444980 MonoBehaviour: m_ObjectHideFlags: 0 @@ -797,6 +798,10 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: gun: {fileID: 3682030003239559187} + zOffset: 0 + returnForce: 0.006 + impulsForce: 0.03 + maxRecoil: 0.1 --- !u!1 &6272346181657429393 GameObject: m_ObjectHideFlags: 0 @@ -1076,7 +1081,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: d1ac7df45c0d7e84993ff57afe3c4d6f, type: 3} propertyPath: m_LocalRotation.x - value: -0 + value: 0 objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: d1ac7df45c0d7e84993ff57afe3c4d6f, type: 3} propertyPath: m_LocalRotation.y @@ -1084,7 +1089,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: d1ac7df45c0d7e84993ff57afe3c4d6f, type: 3} propertyPath: m_LocalRotation.z - value: -0 + value: 0 objectReference: {fileID: 0} - target: {fileID: -8679921383154817045, guid: d1ac7df45c0d7e84993ff57afe3c4d6f, type: 3} propertyPath: m_LocalEulerAnglesHint.x diff --git a/Assets/Scripts/Player/PlayerController.cs b/Assets/Scripts/Player/PlayerController.cs index f93611d..afc7210 100644 --- a/Assets/Scripts/Player/PlayerController.cs +++ b/Assets/Scripts/Player/PlayerController.cs @@ -102,7 +102,7 @@ public class PlayerController : NetworkBehaviour if (Input.GetAxisRaw("Sprint") > 0 && isGrounded) { - Debug.Log("Sprint"); + //Debug.Log("Sprint"); movementSpeed = sprintSpeed; isSprinting = true; } @@ -115,11 +115,11 @@ public class PlayerController : NetworkBehaviour //Grounded if (velocityY < 0) { - velocityY += gravity * Time.deltaTime; + velocityY += gravity * 0.9f * Time.deltaTime; } else { - velocityY += gravity * 1.0f * Time.deltaTime; + velocityY += gravity* Time.deltaTime; } if (isGrounded && velocityY < 0) velocityY = 0.0f; diff --git a/Assets/Scripts/Weapons/Shoot.cs b/Assets/Scripts/Weapons/Shoot.cs index ea635c5..c6b68ff 100644 --- a/Assets/Scripts/Weapons/Shoot.cs +++ b/Assets/Scripts/Weapons/Shoot.cs @@ -8,6 +8,7 @@ public class Shoot : NetworkBehaviour [SerializeField] GameObject muzzle; [SerializeField] ShootAnimation shootAnim; [SerializeField] float fireRate; + [SerializeField] GameObject gunHoldPos; private void Start() { @@ -38,6 +39,7 @@ public class Shoot : NetworkBehaviour shootAnimation(); + if (Physics.Raycast(muzzle.transform.position, muzzle.transform.forward, out hit)) { if (hit.transform.gameObject.GetComponent() != null) @@ -53,7 +55,7 @@ public class Shoot : NetworkBehaviour // This code will be executed on the Client. void shootAnimation() { - shootAnim.StartShootAnimation(fireRate); - + //shootAnim.StartShootAnimation(fireRate); + shootAnim.recoil(gunHoldPos,0.1f); } } diff --git a/Assets/Scripts/Weapons/ShootAnimation.cs b/Assets/Scripts/Weapons/ShootAnimation.cs index 931437a..3e0d108 100644 --- a/Assets/Scripts/Weapons/ShootAnimation.cs +++ b/Assets/Scripts/Weapons/ShootAnimation.cs @@ -5,22 +5,61 @@ using UnityEngine; public class ShootAnimation : MonoBehaviour { private Animator anim; - [SerializeField]private GameObject gun; + [SerializeField] private GameObject gun; + private Transform startTransform; + Vector3 startPos; + Vector3 startRot; + [SerializeField] float zOffset = 0f; + [SerializeField] float returnForce = 0.06f; + [SerializeField] float impulsForce = 0.2f; + [SerializeField] float maxRecoil = 0.5f; + private int recoilCounter = 0; + + float zVelocity = 0f; + public void OnSwitchWeapon(float fireRate) { //gun = newGun; anim = gun.GetComponent(); anim.SetFloat("ShootSpeed",1f/(60f/fireRate)); + startPos = gun.transform.localPosition; + startRot = gun.transform.localRotation.eulerAngles; + } - public void StartShootAnimation(float timeInSeconds) + + + public void recoil(GameObject gun, float force) { - Debug.Log(anim.GetFloat("ShootSpeed")); anim.Play("Shoot"); + recoilCounter++; } - // Update is called once per frame - void Update() + + + void FixedUpdate() { - + //Apply recoil based on the number of shots fired + for (int i = 0; i < recoilCounter; i++) + { + zVelocity -= impulsForce * 0.9f + impulsForce * 0.1f * Mathf.PerlinNoise(i,1f); + } + recoilCounter = 0; + + + zOffset += zVelocity; + + if (zOffset > 0) + { + zOffset = 0f; + zVelocity = 0f; + } + else if (zOffset < 0) + { + zVelocity += returnForce * 0.9f + returnForce * 0.1f * Mathf.PerlinNoise(Time.time,1f); + + } + zOffset = Mathf.Clamp(zOffset,-maxRecoil * 0.5f + -maxRecoil * 0.5f * Mathf.PerlinNoise(Time.time * 1000,1),0); + gun.transform.localPosition = startPos + new Vector3(0,0,zOffset); + gun.transform.localRotation = Quaternion.Euler(startRot.x,startRot.y,startRot.z + zOffset * 50f); } } diff --git a/UserSettings/Layouts/CurrentMaximizeLayout.dwlt b/UserSettings/Layouts/CurrentMaximizeLayout.dwlt index 0b4260b..b8872d4 100644 --- a/UserSettings/Layouts/CurrentMaximizeLayout.dwlt +++ b/UserSettings/Layouts/CurrentMaximizeLayout.dwlt @@ -97,7 +97,7 @@ MonoBehaviour: y: 21 width: 330.8 height: 428.4 - m_Scale: {x: 1.1592737, y: 1.1592739} + m_Scale: {x: 1, y: 1} m_Translation: {x: 165.4, y: 214.2} m_MarginLeft: 0 m_MarginRight: 0 @@ -105,10 +105,10 @@ MonoBehaviour: m_MarginBottom: 0 m_LastShownAreaInsideMargins: serializedVersion: 2 - x: -142.67552 - y: -184.77083 - width: 285.35104 - height: 369.54166 + x: -165.4 + y: -214.2 + width: 330.8 + height: 428.4 m_MinimalGUI: 1 m_defaultScale: 1 m_LastWindowPixelSize: {x: 413.5, y: 561.75} @@ -227,7 +227,7 @@ MonoBehaviour: scrollPos: {x: 0, y: 0} m_SelectedIDs: m_LastClickedID: 0 - m_ExpandedIDs: 660bffff7a0bffff800bffffac0bffff540fffff680fffff6e0fffff9a0fffffd842ffffec42fffff242ffff1e43ffff4c58ffff6058ffff6658ffff9258ffff6876ffff7c76ffff8276ffffae76ffff8cceffff96ceffff9aceffffc6ceffff7ef2ffff84f2ffffe0f3ffffe4f3ffffeef3fffff2f3ffff1ef4ffffdcfaffffeafaffffeefaffff1afbffff2afbffff + m_ExpandedIDs: 141dfcff421dfcfff21dfcff0678fcff1a78fcff2078fcff4e78fcfffe78fcff0082fcff0682fcff3482fcffe482fcffd4c2fcffdac2fcff08c3fcffb8c3fcff0050fdff2e50fdffde50fdff96ddfdff9cddfdffcaddfdff7adefdff92e3fdff98e3fdffc6e3fdff76e4fdffda01feffe001feff0e02feffbe02feff7a0dfeff800dfeffae0dfeff5e0efeffae18feffb418feffe218feff9219feffa22dfefff23ffeff0640feff0c40feff3a40feffea40feffb8a4feffbea4feffeca4feff9ca5feff660bffff7a0bffff800bffffac0bffff540fffff680fffff6e0fffff9a0fffffd842ffffec42fffff242ffff1e43ffff4c58ffff6058ffff6658ffff9258ffff6876ffff7c76ffff8276ffffae76ffff8cceffff96ceffff9aceffffc6ceffff7ef2ffff84f2ffffe0f3ffffe4f3ffffeef3fffff2f3ffff1ef4ffffdcfaffffeafaffffeefaffff1afbffff2afbffff m_RenameOverlay: m_UserAcceptedRename: 0 m_Name: @@ -298,10 +298,10 @@ MonoBehaviour: m_Tooltip: m_Pos: serializedVersion: 2 - x: 297.6 + x: 492 y: 73.6 - width: 207.6 - height: 450 + width: 345.2 + height: 449.4 m_ViewDataDictionary: {fileID: 0} m_OverlayCanvas: m_LastAppliedPresetName: Default @@ -310,14 +310,14 @@ MonoBehaviour: m_KeySerializationHelper: - {fileID: 7614406280852874800, guid: daf332133edb41440a3abb1cf6e24558, type: 2} m_ValueSerializationHelper: - - e00: 0.93576187 + - e00: 0.46289816 e01: 0 e02: 0 - e03: -345.5047 + e03: -98.212 e10: 0 - e11: 0.93576187 + e11: 0.46289816 e12: 0 - e13: 81.44865 + e13: 88.08187 e20: 0 e21: 0 e22: 1 @@ -339,7 +339,7 @@ MonoBehaviour: m_MiniTool: 0 m_LockTracker: m_IsLocked: 0 - m_CurrentEditor: 0 + m_CurrentEditor: 1 m_LayerEditor: m_SelectedLayerIndex: 0 --- !u!114 &9 @@ -578,9 +578,9 @@ MonoBehaviour: m_PlayAudio: 0 m_AudioPlay: 0 m_Position: - m_Target: {x: 0.5076783, y: 3.9909852, z: 0.061484996} + m_Target: {x: 0.49072248, y: 7.6249065, z: 1.8747175} speed: 2 - m_Value: {x: 0.5076783, y: 3.9909852, z: 0.061484996} + m_Value: {x: 0.49072248, y: 7.6249065, z: 1.8747175} m_RenderMode: 0 m_CameraMode: drawMode: 0 @@ -627,13 +627,13 @@ MonoBehaviour: m_GridAxis: 1 m_gridOpacity: 0.5 m_Rotation: - m_Target: {x: -0.013870413, y: 0.90260464, z: 0.029275607, w: 0.42936158} + m_Target: {x: 0.02317691, y: 0.96904457, z: -0.09976932, w: 0.22486019} speed: 2 - m_Value: {x: -0.013869748, y: 0.90256137, z: 0.029274205, w: 0.42934102} + m_Value: {x: 0.023026071, y: 0.96933204, z: -0.09979895, w: 0.22339514} m_Size: - m_Target: 0.22076394 + m_Target: 1.7877662 speed: 2 - m_Value: 0.22076394 + m_Value: 1.7107811 m_Ortho: m_Target: 1 speed: 2 @@ -694,7 +694,7 @@ MonoBehaviour: m_Enabled: 1 m_EditorHideFlags: 1 m_Script: {fileID: 12006, guid: 0000000000000000e000000000000000, type: 0} - m_Name: ConsoleWindow + m_Name: ProjectBrowser m_EditorClassIdentifier: m_Children: [] m_Position: @@ -703,14 +703,14 @@ MonoBehaviour: y: 470.4 width: 1172 height: 260.4 - m_MinSize: {x: 101, y: 121} - m_MaxSize: {x: 4001, y: 4021} - m_ActualView: {fileID: 13} + m_MinSize: {x: 231, y: 271} + m_MaxSize: {x: 10001, y: 10021} + m_ActualView: {fileID: 12} m_Panes: - {fileID: 12} - {fileID: 13} - m_Selected: 1 - m_LastSelected: 0 + m_Selected: 0 + m_LastSelected: 1 --- !u!114 &12 MonoBehaviour: m_ObjectHideFlags: 52 @@ -764,10 +764,10 @@ MonoBehaviour: m_LockTracker: m_IsLocked: 0 m_FolderTreeState: - scrollPos: {x: 0, y: 185} + scrollPos: {x: 0, y: 179} m_SelectedIDs: 6a670000 m_LastClickedID: 26474 - m_ExpandedIDs: 000000004e62000050620000606200006262000066620000b8620000f462000000ca9a3bffffff7f + m_ExpandedIDs: 000000004e62000050620000606200006262000000ca9a3bffffff7f m_RenameOverlay: m_UserAcceptedRename: 0 m_Name: @@ -795,7 +795,7 @@ MonoBehaviour: scrollPos: {x: 0, y: 0} m_SelectedIDs: m_LastClickedID: 0 - m_ExpandedIDs: 000000004e62000050620000 + m_ExpandedIDs: 000000004e620000506200006062000000ca9a3bffffff7f m_RenameOverlay: m_UserAcceptedRename: 0 m_Name: @@ -822,8 +822,8 @@ MonoBehaviour: m_ListAreaState: m_SelectedInstanceIDs: m_LastClickedInstanceID: 0 - m_HadKeyboardFocusLastEvent: 1 - m_ExpandedInstanceIDs: c623000030660000986f0000d65e0000 + m_HadKeyboardFocusLastEvent: 0 + m_ExpandedInstanceIDs: c623000030660000986f0000d65e0000825f0000 m_RenameOverlay: m_UserAcceptedRename: 0 m_Name: @@ -898,8 +898,8 @@ MonoBehaviour: y: 0 width: 364 height: 730.8 - m_MinSize: {x: 275, y: 50} - m_MaxSize: {x: 4000, y: 4000} + m_MinSize: {x: 276, y: 71} + m_MaxSize: {x: 4001, y: 4021} m_ActualView: {fileID: 15} m_Panes: - {fileID: 15}