diff --git a/Assets/Prefabs/BasicEnemy.prefab b/Assets/Prefabs/BasicEnemy.prefab index 96e1e22..0236986 100644 --- a/Assets/Prefabs/BasicEnemy.prefab +++ b/Assets/Prefabs/BasicEnemy.prefab @@ -15,6 +15,10 @@ GameObject: - component: {fileID: 5624618021356392161} - component: {fileID: 5624618021356392160} - component: {fileID: 5624618021356392169} + - component: {fileID: 7190414835926678604} + - component: {fileID: 6296776006770892297} + - component: {fileID: 7675101942776953993} + - component: {fileID: 3479255712880044547} m_Layer: 10 m_Name: BasicEnemy m_TagString: Enemy @@ -147,8 +151,6 @@ MonoBehaviour: playerBehaviour: {fileID: 0} attackDamage: 10 timeBetweenAttacks: 1 - defaultMaterial: {fileID: 10303, guid: 0000000000000000f000000000000000, type: 0} - slowMotionMaterial: {fileID: 2100000, guid: e9b9f1aca412023479a266fd1694c1d8, type: 2} sightRange: 20 attackRange: 1 playerInSightRange: 0 @@ -166,3 +168,59 @@ MonoBehaviour: m_Name: m_EditorClassIdentifier: health: 10 +--- !u!114 &7190414835926678604 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 5624618021356392095} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 2e99aeb51475d4744aef1a65ffa0eeb4, type: 3} + m_Name: + m_EditorClassIdentifier: +--- !u!54 &6296776006770892297 +Rigidbody: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 5624618021356392095} + serializedVersion: 2 + m_Mass: 1 + m_Drag: 0 + m_AngularDrag: 0 + m_UseGravity: 0 + m_IsKinematic: 1 + m_Interpolate: 0 + m_Constraints: 112 + m_CollisionDetection: 0 +--- !u!114 &7675101942776953993 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 5624618021356392095} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 8241bf709a634454d8c669de37013737, type: 3} + m_Name: + m_EditorClassIdentifier: + knockbackThreshold: 0.05 + recoveryDelay: 0.5 +--- !u!114 &3479255712880044547 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 5624618021356392095} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 8241bf709a634454d8c669de37013737, type: 3} + m_Name: + m_EditorClassIdentifier: + knockbackThreshold: 0.05 + recoveryDelay: 0.5 diff --git a/Assets/Prefabs/Blast.prefab b/Assets/Prefabs/Blast.prefab new file mode 100644 index 0000000..a62c35f --- /dev/null +++ b/Assets/Prefabs/Blast.prefab @@ -0,0 +1,159 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!1 &8645659478751323567 +GameObject: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + serializedVersion: 6 + m_Component: + - component: {fileID: 8645659478751323564} + - component: {fileID: 8645659478751323565} + - component: {fileID: 8645659478751323562} + m_Layer: 0 + m_Name: Blast + m_TagString: Untagged + m_Icon: {fileID: 0} + m_NavMeshLayer: 0 + m_StaticEditorFlags: 0 + m_IsActive: 1 +--- !u!4 &8645659478751323564 +Transform: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 8645659478751323567} + m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} + m_LocalPosition: {x: 0, y: 0, z: 0} + m_LocalScale: {x: 1, y: 1, z: 1} + m_ConstrainProportionsScale: 0 + m_Children: [] + m_Father: {fileID: 0} + m_RootOrder: 0 + m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} +--- !u!120 &8645659478751323565 +LineRenderer: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 8645659478751323567} + m_Enabled: 1 + m_CastShadows: 1 + m_ReceiveShadows: 1 + m_DynamicOccludee: 1 + m_StaticShadowCaster: 0 + m_MotionVectors: 0 + m_LightProbeUsage: 0 + m_ReflectionProbeUsage: 0 + m_RayTracingMode: 0 + m_RayTraceProcedural: 0 + m_RenderingLayerMask: 1 + m_RendererPriority: 0 + m_Materials: + - {fileID: 2100000, guid: 3dc0058389e20b64b942621291c83d46, type: 2} + m_StaticBatchInfo: + firstSubMesh: 0 + subMeshCount: 0 + m_StaticBatchRoot: {fileID: 0} + m_ProbeAnchor: {fileID: 0} + m_LightProbeVolumeOverride: {fileID: 0} + m_ScaleInLightmap: 1 + m_ReceiveGI: 1 + m_PreserveUVs: 0 + m_IgnoreNormalsForChartDetection: 0 + m_ImportantGI: 0 + m_StitchLightmapSeams: 1 + m_SelectedEditorRenderState: 3 + m_MinimumChartSize: 4 + m_AutoUVMaxDistance: 0.5 + m_AutoUVMaxAngle: 89 + m_LightmapParameters: {fileID: 0} + m_SortingLayerID: 0 + m_SortingLayer: 0 + m_SortingOrder: 0 + m_Positions: + - {x: 0, y: 0, z: 0} + - {x: 0, y: 0, z: 1} + m_Parameters: + serializedVersion: 3 + widthMultiplier: 3 + widthCurve: + serializedVersion: 2 + m_Curve: + - serializedVersion: 3 + time: 0 + value: 1 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.33333334 + - serializedVersion: 3 + time: 1 + value: 1 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0 + outWeight: 0 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 + colorGradient: + serializedVersion: 2 + key0: {r: 1, g: 1, b: 1, a: 1} + key1: {r: 1, g: 1, b: 1, a: 1} + key2: {r: 0, g: 0, b: 0, a: 0} + key3: {r: 0, g: 0, b: 0, a: 0} + key4: {r: 0, g: 0, b: 0, a: 0} + key5: {r: 0, g: 0, b: 0, a: 0} + key6: {r: 0, g: 0, b: 0, a: 0} + key7: {r: 0, g: 0, b: 0, a: 0} + ctime0: 0 + ctime1: 65535 + ctime2: 0 + ctime3: 0 + ctime4: 0 + ctime5: 0 + ctime6: 0 + ctime7: 0 + atime0: 0 + atime1: 65535 + atime2: 0 + atime3: 0 + atime4: 0 + atime5: 0 + atime6: 0 + atime7: 0 + m_Mode: 0 + m_NumColorKeys: 2 + m_NumAlphaKeys: 2 + numCornerVertices: 0 + numCapVertices: 0 + alignment: 0 + textureMode: 0 + shadowBias: 0.5 + generateLightingData: 0 + m_UseWorldSpace: 0 + m_Loop: 0 +--- !u!114 &8645659478751323562 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 8645659478751323567} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: 7b440a43394e3404abe7e7e1f3d4ffa7, type: 3} + m_Name: + m_EditorClassIdentifier: + speed: 0 + blastForce: 0 + maxRadius: 0 + height: 0.5 diff --git a/Assets/Prefabs/Blast.prefab.meta b/Assets/Prefabs/Blast.prefab.meta new file mode 100644 index 0000000..38033bc --- /dev/null +++ b/Assets/Prefabs/Blast.prefab.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: d7d32f5b5f2560d4db3a2292d002b9de +PrefabImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scenes/Main.unity b/Assets/Scenes/Main.unity index 73a9f51..1b7859c 100644 --- a/Assets/Scenes/Main.unity +++ b/Assets/Scenes/Main.unity @@ -302,7 +302,7 @@ Transform: m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 358058293} - m_RootOrder: 0 + m_RootOrder: 1 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &87152847 GameObject: @@ -1603,7 +1603,6 @@ GameObject: m_Component: - component: {fileID: 340294615} - component: {fileID: 340294618} - - component: {fileID: 340294617} m_Layer: 7 m_Name: EnemyNavWall m_TagString: Wall @@ -1624,49 +1623,8 @@ Transform: m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 358058293} - m_RootOrder: 1 + m_RootOrder: 0 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} ---- !u!23 &340294617 -MeshRenderer: - m_ObjectHideFlags: 0 - m_CorrespondingSourceObject: {fileID: 0} - m_PrefabInstance: {fileID: 0} - m_PrefabAsset: {fileID: 0} - m_GameObject: {fileID: 340294614} - m_Enabled: 1 - m_CastShadows: 0 - m_ReceiveShadows: 0 - m_DynamicOccludee: 0 - m_StaticShadowCaster: 0 - m_MotionVectors: 1 - m_LightProbeUsage: 0 - m_ReflectionProbeUsage: 0 - m_RayTracingMode: 2 - m_RayTraceProcedural: 0 - m_RenderingLayerMask: 1 - m_RendererPriority: 0 - m_Materials: [] - m_StaticBatchInfo: - firstSubMesh: 0 - subMeshCount: 0 - m_StaticBatchRoot: {fileID: 0} - m_ProbeAnchor: {fileID: 0} - m_LightProbeVolumeOverride: {fileID: 0} - m_ScaleInLightmap: 1 - m_ReceiveGI: 1 - m_PreserveUVs: 0 - m_IgnoreNormalsForChartDetection: 0 - m_ImportantGI: 0 - m_StitchLightmapSeams: 1 - m_SelectedEditorRenderState: 3 - m_MinimumChartSize: 4 - m_AutoUVMaxDistance: 0.5 - m_AutoUVMaxAngle: 89 - m_LightmapParameters: {fileID: 0} - m_SortingLayerID: 0 - m_SortingLayer: 0 - m_SortingOrder: 0 - m_AdditionalVertexStreams: {fileID: 0} --- !u!33 &340294618 MeshFilter: m_ObjectHideFlags: 0 @@ -1721,7 +1679,7 @@ Transform: m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 17 + m_RootOrder: 18 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!114 &346416859 MonoBehaviour: @@ -1781,8 +1739,8 @@ Transform: m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: - - {fileID: 62711085} - {fileID: 340294615} + - {fileID: 62711085} - {fileID: 276982732} - {fileID: 752645536} - {fileID: 924598920} @@ -3989,9 +3947,8 @@ Transform: - {fileID: 370204382} - {fileID: 327132825} - {fileID: 1735663483} - - {fileID: 1052042176} m_Father: {fileID: 0} - m_RootOrder: 11 + m_RootOrder: 12 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &814273024 GameObject: @@ -4992,8 +4949,8 @@ Transform: - {fileID: 109199959} - {fileID: 1021490655} - {fileID: 1519121681} - m_Father: {fileID: 792076353} - m_RootOrder: 4 + m_Father: {fileID: 0} + m_RootOrder: 11 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1053413785 GameObject: @@ -5401,10 +5358,6 @@ PrefabInstance: propertyPath: m_Name value: Bottle objectReference: {fileID: 0} - - target: {fileID: 4888753569647563423, guid: f2cfd694f3c29e84db23cdcaf1a2c3c8, type: 3} - propertyPath: m_Enabled - value: 0 - objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: f2cfd694f3c29e84db23cdcaf1a2c3c8, type: 3} --- !u!4 &1067012785 stripped @@ -5758,7 +5711,7 @@ RectTransform: - {fileID: 1611141925} - {fileID: 673075237} m_Father: {fileID: 0} - m_RootOrder: 15 + m_RootOrder: 16 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} m_AnchorMin: {x: 0, y: 0} m_AnchorMax: {x: 0, y: 0} @@ -5884,7 +5837,7 @@ Transform: m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 0} - m_RootOrder: 12 + m_RootOrder: 13 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1147868650 GameObject: @@ -6603,7 +6556,7 @@ Transform: - {fileID: 685889367} - {fileID: 818362641} m_Father: {fileID: 0} - m_RootOrder: 13 + m_RootOrder: 14 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1358383472 GameObject: @@ -6976,19 +6929,19 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 3663668822022932514, guid: f2cfd694f3c29e84db23cdcaf1a2c3c8, type: 3} propertyPath: m_LocalPosition.x - value: 127.143 + value: 0 objectReference: {fileID: 0} - target: {fileID: 3663668822022932514, guid: f2cfd694f3c29e84db23cdcaf1a2c3c8, type: 3} propertyPath: m_LocalPosition.y - value: 75.008 + value: 60.063 objectReference: {fileID: 0} - target: {fileID: 3663668822022932514, guid: f2cfd694f3c29e84db23cdcaf1a2c3c8, type: 3} propertyPath: m_LocalPosition.z - value: 25 + value: -100 objectReference: {fileID: 0} - target: {fileID: 3663668822022932514, guid: f2cfd694f3c29e84db23cdcaf1a2c3c8, type: 3} propertyPath: m_LocalRotation.w - value: 0.89493436 + value: 0.7071068 objectReference: {fileID: 0} - target: {fileID: 3663668822022932514, guid: f2cfd694f3c29e84db23cdcaf1a2c3c8, type: 3} propertyPath: m_LocalRotation.x @@ -7000,7 +6953,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 3663668822022932514, guid: f2cfd694f3c29e84db23cdcaf1a2c3c8, type: 3} propertyPath: m_LocalRotation.z - value: -0.44619772 + value: 0.7071068 objectReference: {fileID: 0} - target: {fileID: 3663668822022932514, guid: f2cfd694f3c29e84db23cdcaf1a2c3c8, type: 3} propertyPath: m_LocalEulerAnglesHint.x @@ -7012,16 +6965,12 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 3663668822022932514, guid: f2cfd694f3c29e84db23cdcaf1a2c3c8, type: 3} propertyPath: m_LocalEulerAnglesHint.z - value: -53 + value: 90 objectReference: {fileID: 0} - target: {fileID: 3663668822022932518, guid: f2cfd694f3c29e84db23cdcaf1a2c3c8, type: 3} propertyPath: m_Name value: Bottle objectReference: {fileID: 0} - - target: {fileID: 4888753569647563423, guid: f2cfd694f3c29e84db23cdcaf1a2c3c8, type: 3} - propertyPath: m_Enabled - value: 0 - objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: f2cfd694f3c29e84db23cdcaf1a2c3c8, type: 3} --- !u!4 &1459615225 stripped @@ -7935,7 +7884,7 @@ RectTransform: m_Children: - {fileID: 259179196} m_Father: {fileID: 0} - m_RootOrder: 16 + m_RootOrder: 17 m_LocalEulerAnglesHint: {x: -30, y: 120, z: 0} m_AnchorMin: {x: 0, y: 0} m_AnchorMax: {x: 0, y: 0} @@ -8379,7 +8328,7 @@ Transform: - {fileID: 1067012785} - {fileID: 1459615225} m_Father: {fileID: 0} - m_RootOrder: 14 + m_RootOrder: 15 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} --- !u!1 &1771448631 GameObject: @@ -9848,9 +9797,7 @@ PrefabInstance: propertyPath: m_Enabled value: 0 objectReference: {fileID: 0} - m_RemovedComponents: - - {fileID: 2243451592876107041, guid: b9d0709db02ef5344af36b009a22174f, type: 3} - - {fileID: 2243451592876107042, guid: b9d0709db02ef5344af36b009a22174f, type: 3} + m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: b9d0709db02ef5344af36b009a22174f, type: 3} --- !u!114 &2243451593235947852 stripped MonoBehaviour: @@ -10174,10 +10121,6 @@ PrefabInstance: propertyPath: m_Name value: Bottle objectReference: {fileID: 0} - - target: {fileID: 4888753569647563423, guid: f2cfd694f3c29e84db23cdcaf1a2c3c8, type: 3} - propertyPath: m_Enabled - value: 0 - objectReference: {fileID: 0} m_RemovedComponents: [] m_SourcePrefab: {fileID: 100100000, guid: f2cfd694f3c29e84db23cdcaf1a2c3c8, type: 3} --- !u!4 &3745622628223611181 diff --git a/Assets/Scripts/BasicEnemyController.cs b/Assets/Scripts/BasicEnemyController.cs index 8f5a24b..d762e80 100644 --- a/Assets/Scripts/BasicEnemyController.cs +++ b/Assets/Scripts/BasicEnemyController.cs @@ -52,7 +52,10 @@ private void SearchWalkPoint() if (Physics.Raycast(walkPoint, -transform.up, 2f, groundLayer)) walkPointSet = true; } - private void ChasePlayer() => agent.SetDestination(player.position); + private void ChasePlayer() + { + if (agent.enabled) agent.SetDestination(player.position); + } private void AttackPlayer() { diff --git a/Assets/Scripts/Blast.cs b/Assets/Scripts/Blast.cs new file mode 100644 index 0000000..e6ebbb2 --- /dev/null +++ b/Assets/Scripts/Blast.cs @@ -0,0 +1,118 @@ +using UnityEngine; +using UnityEngine.AI; + +[RequireComponent(typeof(LineRenderer))] +public class Blast : MonoBehaviour +{ + [Header("Blast Configuration")] + public float speed; + public float blastForce; + public float maxRadius; + public float height; + + private LineRenderer lr; + private float radius; + private int pointCount = 30; + private float pointsAngle; + private Vector3[] positions = new Vector3[30]; + private Collider[] col; + private bool isExpanding = true; + + void Start() + { + lr = GetComponent(); + lr.positionCount = pointCount; + FindPoints(); + } + + void FindPoints() + { + pointsAngle = 360f / pointCount; + for (int i = 0; i < pointCount; i++) + { + float angle = pointsAngle * i * Mathf.Deg2Rad; + positions[i] = new Vector3(Mathf.Sin(angle), 0, Mathf.Cos(angle)); + } + positions[pointCount - 1] = positions[0]; + } + + void FixedUpdate() + { + if (isExpanding) + { + if (radius <= maxRadius) + { + ApplyForce(); + SetPositions(); + radius += speed; + lr.widthMultiplier = (maxRadius - radius) / maxRadius; + } + else + { + isExpanding = false; + Destroy(gameObject); + } + } + } + + void SetPositions() + { + for (int i = 0; i < pointCount; i++) + { + lr.SetPosition(i, positions[i] * radius); + } + } + + void ApplyForce() + { + Vector3 halfExtents = new Vector3(radius, height / 2f, radius); + col = Physics.OverlapBox(transform.position, halfExtents, Quaternion.identity); + foreach (Collider c in col) + { + Vector3 direction = (c.transform.position - transform.position).normalized; + Vector3 force = direction * blastForce; + + if (c.TryGetComponent(out KnockbackHandler knockbackHandler)) + { + knockbackHandler.ApplyKnockback(force); + } + else if (c.TryGetComponent(out Rigidbody colRb)) + { + colRb.AddForce(force, ForceMode.Impulse); + } + } + } + + void OnDrawGizmosSelected() + { + Gizmos.color = Color.red; + Gizmos.matrix = transform.localToWorldMatrix; + DrawWireCylinder(Vector3.zero, maxRadius, height); + } + + void DrawWireCylinder(Vector3 center, float radius, float height) + { + float halfHeight = height / 2f; + DrawCircle(center + Vector3.up * halfHeight, radius); + DrawCircle(center - Vector3.up * halfHeight, radius); + for (int i = 0; i < 4; i++) + { + float angle = i * Mathf.PI / 2; + Vector3 pos = new Vector3(Mathf.Cos(angle) * radius, 0, Mathf.Sin(angle) * radius); + Gizmos.DrawLine(center + pos + Vector3.up * halfHeight, center + pos - Vector3.up * halfHeight); + } + } + + void DrawCircle(Vector3 center, float radius) + { + int segments = 32; + Vector3 prevPos = center + new Vector3(radius, 0, 0); + for (int i = 1; i <= segments; i++) + { + float angle = i * Mathf.PI * 2f / segments; + Vector3 pos = center + new Vector3(Mathf.Cos(angle) * radius, 0, Mathf.Sin(angle) * radius); + Gizmos.DrawLine(prevPos, pos); + prevPos = pos; + } + } +} \ No newline at end of file diff --git a/Assets/Scripts/Blast.cs.meta b/Assets/Scripts/Blast.cs.meta new file mode 100644 index 0000000..537877d --- /dev/null +++ b/Assets/Scripts/Blast.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 7b440a43394e3404abe7e7e1f3d4ffa7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/KnockbackHandler.cs b/Assets/Scripts/KnockbackHandler.cs new file mode 100644 index 0000000..eb2559f --- /dev/null +++ b/Assets/Scripts/KnockbackHandler.cs @@ -0,0 +1,58 @@ +using UnityEngine; +using UnityEngine.AI; + +[RequireComponent(typeof(NavMeshAgent))] +[RequireComponent(typeof(Rigidbody))] +public class KnockbackHandler : MonoBehaviour +{ + public float knockbackThreshold = 0.05f; + public float recoveryDelay = 0.5f; + + private NavMeshAgent agent; + private Rigidbody rb; + private bool isKnockedBack = false; + + + private void Awake() + { + agent = GetComponent(); + rb = GetComponent(); + rb.isKinematic = true; + } + + private void Update() + { + if (isKnockedBack) CheckKnockbackEnd(); + } + + public void ApplyKnockback(Vector3 force) + { + if (!isKnockedBack) + { + isKnockedBack = true; + agent.enabled = false; + rb.isKinematic = false; + rb.AddForce(force, ForceMode.Impulse); + Invoke(nameof(StartRecovery), recoveryDelay); + } + } + + private void StartRecovery() + { + rb.velocity = Vector3.zero; + rb.angularVelocity = Vector3.zero; + } + + private void CheckKnockbackEnd() + { + if (rb.velocity.magnitude <= knockbackThreshold) EndKnockback(); + } + + private void EndKnockback() + { + isKnockedBack = false; + rb.isKinematic = true; + agent.enabled = true; + agent.Warp(transform.position); + } +} \ No newline at end of file diff --git a/Assets/Scripts/KnockbackHandler.cs.meta b/Assets/Scripts/KnockbackHandler.cs.meta new file mode 100644 index 0000000..eeb4f06 --- /dev/null +++ b/Assets/Scripts/KnockbackHandler.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8241bf709a634454d8c669de37013737 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Sliding.cs b/Assets/Scripts/Sliding.cs index 6251e4b..5181e5a 100644 --- a/Assets/Scripts/Sliding.cs +++ b/Assets/Scripts/Sliding.cs @@ -16,9 +16,11 @@ public class Sliding : MonoBehaviour public float rotationSpeed; [Header("Ground Slam")] + public GameObject blastPrefab; public float groundSlamForce; public float shakeDuration; public float shakeIntensity; + public float blastRadius = 5f; private float startYscale; private Vector3 slideDirection; @@ -43,9 +45,7 @@ void Update() } if ((Input.GetKeyUp(KeyCode.LeftControl) && playerController.sliding) - - || (!playerController.grounded && playerController.sliding)) - StopSlide(); + || (!playerController.grounded && playerController.sliding)) StopSlide(); if (CanRestorePlayerHeight() && !playerController.sliding) RestorePlayerHeight(); if (playerController.sliding) HandleSlidingMovement(); @@ -53,6 +53,7 @@ void Update() if (isGroundSlamming && playerController.grounded) { cam.DoShake(shakeDuration, shakeIntensity); + SpawnBlast(); isGroundSlamming = false; } } @@ -78,6 +79,18 @@ private void GroundSlam() isGroundSlamming = true; } + private void SpawnBlast() + { + GameObject blastObject = Instantiate(blastPrefab, transform.position, Quaternion.identity); + + if (blastObject.TryGetComponent(out var blast)) + { + blast.maxRadius = blastRadius; + blast.speed = .5f; + blast.blastForce = groundSlamForce; + } + } + private void HandleSlidingMovement() { float horizontalInput = Input.GetAxisRaw("Horizontal"); @@ -98,9 +111,9 @@ private void HandleSlidingMovement() private void SlidingMovement() { Vector3 forceToApply; - if (playerController.OnSlope()) - forceToApply = playerController.GetSlopeMoveDirection(slideDirection) * slideForce; + if (playerController.OnSlope()) forceToApply = playerController.GetSlopeMoveDirection(slideDirection) * slideForce; else forceToApply = slideDirection * slideForce; + rb.AddForce(forceToApply, ForceMode.Force); playerObject.forward = Vector3.Slerp(playerObject.forward, slideDirection, Time.deltaTime * 10f); } @@ -118,12 +131,11 @@ private bool CanRestorePlayerHeight() Vector3 playerCenter = transform.position + (Vector3.up * (playerObject.localScale.y / 2f)); float playerHalfNormalHeight = startYscale / 2f; - RaycastHit hit; - if (Physics.Raycast(playerCenter, Vector3.up, out hit, playerHalfNormalHeight + 0.1f, playerController.groundLayer)) + if (Physics.Raycast(playerCenter, Vector3.up, out RaycastHit hit, playerHalfNormalHeight + 0.1f, playerController.groundLayer)) return hit.distance >= startYscale; + return true; } - private void RestorePlayerHeight() => - playerObject.localScale = new Vector3(playerObject.localScale.x, startYscale, playerObject.localScale.z); + private void RestorePlayerHeight() => playerObject.localScale = new Vector3(playerObject.localScale.x, startYscale, playerObject.localScale.z); } \ No newline at end of file