diff --git a/Assets/CameraShake.cs b/Assets/CameraShake.cs new file mode 100644 index 0000000..f997485 --- /dev/null +++ b/Assets/CameraShake.cs @@ -0,0 +1,36 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +public class CameraShake : MonoBehaviour { + + [Header("Camera Shake Info")] + [SerializeField] bool cameraShakeActive = true; + [SerializeField] Camera objectToMove; + [SerializeField] float cameraShakeRadius = 6f; + [SerializeField] float cameraShakeDuration = 1f; + [SerializeField] AnimationCurve cameraShakeCurve; + + + + void cameraShake() { + Collider[] colliders = Physics.OverlapSphere(transform.position, cameraShakeRadius); + foreach (Collider nearbyObject in colliders) { + if (nearbyObject.GetComponent() && nearbyObject.GetType() != typeof(UnityEngine.CharacterController)) { + // Start coroutine that shakes the camera + StartCoroutine(shaking(nearbyObject)); + } + } + } + + IEnumerator shaking(Collider obj) { + float elapsedTime = 0f; + while (elapsedTime < cameraShakeDuration) { + elapsedTime += Time.deltaTime; + float strength = cameraShakeCurve.Evaluate(elapsedTime / cameraShakeDuration); + objectToMove.transform.localPosition = objectToMove.transform.localPosition + Random.insideUnitSphere * strength; + yield return new WaitForSeconds(Time.deltaTime); + } + yield return null; + } +} diff --git a/Assets/CameraShake.cs.meta b/Assets/CameraShake.cs.meta new file mode 100644 index 0000000..232b31c --- /dev/null +++ b/Assets/CameraShake.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: aee84ae112d831740ac546181a8c23e4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Prefabs/Player/Player.prefab b/Assets/Prefabs/Player/Player.prefab index 29af1f5..761556f 100644 --- a/Assets/Prefabs/Player/Player.prefab +++ b/Assets/Prefabs/Player/Player.prefab @@ -374,6 +374,7 @@ GameObject: - component: {fileID: 8357986889675898169} - component: {fileID: 701870116503968957} - component: {fileID: 7564572533756368624} + - component: {fileID: 6541612578984644575} m_Layer: 0 m_Name: Player m_TagString: Player @@ -525,6 +526,33 @@ MonoBehaviour: AimPoint: {fileID: 2992488853448454767} HoldPoint: {fileID: 8043317747957554736} isAiming: 0 + cameraShakeActive: 1 + objectToMove: {fileID: 6272346181657429395} + cameraShakeDuration: 1 + cameraShakeCurve: + serializedVersion: 2 + m_Curve: + - serializedVersion: 3 + time: -0.4614612 + value: 0.028004631 + inSlope: -0.015037956 + outSlope: -0.015037956 + tangentMode: 0 + weightedMode: 0 + inWeight: 0 + outWeight: 0.28070113 + - serializedVersion: 3 + time: 1.4633081 + value: -0.037871435 + inSlope: -0.011502813 + outSlope: -0.011502813 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.23245634 + outWeight: 0 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 rotationMod: - {x: 0, y: 0, z: 0, w: 0} - {x: 0, y: 0, z: 0, w: 0} @@ -572,8 +600,6 @@ MonoBehaviour: username: usernameTextObj: {fileID: 0} health: 0 - playerNeck: {fileID: 7061967201775281134} - playerCamera: {fileID: 6272346181657429395} --- !u!114 &1258077278480047560 MonoBehaviour: m_ObjectHideFlags: 0 @@ -788,6 +814,55 @@ MonoBehaviour: syncInterval: 0.1 clientAuthority: 1 animator: {fileID: 1978909962} +--- !u!114 &6541612578984644575 +MonoBehaviour: + m_ObjectHideFlags: 0 + m_CorrespondingSourceObject: {fileID: 0} + m_PrefabInstance: {fileID: 0} + m_PrefabAsset: {fileID: 0} + m_GameObject: {fileID: 6272346181302961293} + m_Enabled: 1 + m_EditorHideFlags: 0 + m_Script: {fileID: 11500000, guid: aee84ae112d831740ac546181a8c23e4, type: 3} + m_Name: + m_EditorClassIdentifier: + cameraShakeActive: 1 + objectToMove: {fileID: 6272346181657429395} + cameraShakeRadius: 200 + cameraShakeDuration: 0.3 + cameraShakeCurve: + serializedVersion: 2 + m_Curve: + - serializedVersion: 3 + time: 0 + value: 0 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0 + outWeight: 0 + - serializedVersion: 3 + time: 0.027669588 + value: 0.016447358 + inSlope: -0.015340646 + outSlope: -0.015340646 + tangentMode: 0 + weightedMode: 0 + inWeight: 0.33333334 + outWeight: 0.2301668 + - serializedVersion: 3 + time: 0.6248613 + value: 0.0023211986 + inSlope: 0 + outSlope: 0 + tangentMode: 0 + weightedMode: 0 + inWeight: 0 + outWeight: 0 + m_PreInfinity: 2 + m_PostInfinity: 2 + m_RotationOrder: 4 --- !u!1 &6272346181657429393 GameObject: m_ObjectHideFlags: 0 @@ -1016,8 +1091,8 @@ Transform: m_PrefabInstance: {fileID: 0} m_PrefabAsset: {fileID: 0} m_GameObject: {fileID: 7061967201775281134} - m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} - m_LocalPosition: {x: 0, y: 1.66, z: 0} + m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} + m_LocalPosition: {x: 0, y: 1.6599998, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} m_ConstrainProportionsScale: 0 m_Children: diff --git a/Assets/Scenes/TestScene.unity b/Assets/Scenes/TestScene.unity index a755f28..9497ade 100644 --- a/Assets/Scenes/TestScene.unity +++ b/Assets/Scenes/TestScene.unity @@ -5590,7 +5590,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 732033708985862910, guid: bd217f9f85ce78e46a925776562625e8, type: 3} propertyPath: m_LocalPosition.y - value: 1.347 + value: 1.44 objectReference: {fileID: 0} - target: {fileID: 732033708985862910, guid: bd217f9f85ce78e46a925776562625e8, type: 3} propertyPath: m_LocalPosition.z @@ -5718,11 +5718,11 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} propertyPath: cameraShakeCurve.m_Curve.Array.data[1].time - value: 0.47748455 + value: 0.7053975 objectReference: {fileID: 0} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} propertyPath: cameraShakeCurve.m_Curve.Array.data[2].time - value: 3.651196 + value: 3.7575498 objectReference: {fileID: 0} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} propertyPath: cameraShakeCurve.m_Curve.Array.data[0].value @@ -5730,15 +5730,15 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} propertyPath: cameraShakeCurve.m_Curve.Array.data[1].value - value: 3.9823725 + value: 2.779751 objectReference: {fileID: 0} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} propertyPath: cameraShakeCurve.m_Curve.Array.data[2].value - value: 1.3784328 + value: 0.2894435 objectReference: {fileID: 0} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} propertyPath: cameraShakeCurve.m_Curve.Array.data[0].inSlope - value: 8.401682 + value: 3.9536607 objectReference: {fileID: 0} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} propertyPath: cameraShakeCurve.m_Curve.Array.data[1].inSlope @@ -5746,7 +5746,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} propertyPath: cameraShakeCurve.m_Curve.Array.data[2].inSlope - value: -0.82047147 + value: -0.81591856 objectReference: {fileID: 0} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} propertyPath: cameraShakeCurve.m_Curve.Array.data[0].inWeight @@ -5754,7 +5754,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} propertyPath: cameraShakeCurve.m_Curve.Array.data[0].outSlope - value: 8.401682 + value: 3.9536607 objectReference: {fileID: 0} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} propertyPath: cameraShakeCurve.m_Curve.Array.data[1].inWeight @@ -5770,7 +5770,7 @@ PrefabInstance: objectReference: {fileID: 0} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} propertyPath: cameraShakeCurve.m_Curve.Array.data[2].outSlope - value: -0.82047147 + value: -0.81591856 objectReference: {fileID: 0} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} propertyPath: cameraShakeCurve.m_Curve.Array.data[0].outWeight diff --git a/Assets/Scripts/Player/Player.cs b/Assets/Scripts/Player/Player.cs index bc35965..262af10 100644 --- a/Assets/Scripts/Player/Player.cs +++ b/Assets/Scripts/Player/Player.cs @@ -24,10 +24,6 @@ public class Player : NetworkBehaviour private int kills; private int deaths; - [SerializeField] GameObject playerNeck; - [SerializeField] Camera playerCamera; - public GameObject PlayerNeck { get => playerNeck; set => playerNeck = value; } - public Camera PlayerCamera { get => playerCamera; } private void Start() { diff --git a/Assets/Scripts/Player/ProcedualAnimationController.cs b/Assets/Scripts/Player/ProcedualAnimationController.cs index 0ff5d84..064461c 100644 --- a/Assets/Scripts/Player/ProcedualAnimationController.cs +++ b/Assets/Scripts/Player/ProcedualAnimationController.cs @@ -101,12 +101,41 @@ public class ProcedualAnimationController : NetworkBehaviour [SerializeField] GameObject HoldPoint; public bool isAiming = false; + [Header("Camera Shake Info")] + [SerializeField] bool cameraShakeActive = true; + [SerializeField] Camera objectToMove; + [SerializeField] float cameraShakeDuration = 1f; + [SerializeField] AnimationCurve cameraShakeCurve; + Vector3[] positionMod = new Vector3[4]; public Quaternion[] rotationMod = new Quaternion[4]; public Transform GunRightHandREF { get => gunRightHandREF; set => gunRightHandREF = value; } public Transform GunLeftHandREF { get => gunLeftHandREF; set => gunLeftHandREF = value; } + public void cameraShake() { + // If camera shake is not disabled + if (cameraShakeActive) { + // Start coroutine that shakes the camera + StartCoroutine(shaking()); + } + } + + private IEnumerator shaking() { + float elapsedTime = 0f; + while (elapsedTime < cameraShakeDuration) { + // Time increment + elapsedTime += Time.deltaTime; + // Getting strength value from curve at current time + float strength = cameraShakeCurve.Evaluate(elapsedTime / cameraShakeDuration); + // Move object + objectToMove.transform.localPosition = objectToMove.transform.localPosition + Random.insideUnitSphere * strength; + // Wait + yield return new WaitForSeconds(Time.deltaTime); + } + yield return null; + } + public void walkAnimation() { playerAnimator.SetFloat("x", playerController.localVelocity.x / playerController.currentMaxSpeed); diff --git a/Assets/Scripts/Weapons/Grenade.cs b/Assets/Scripts/Weapons/Grenade.cs index 2897e39..ebed2c2 100644 --- a/Assets/Scripts/Weapons/Grenade.cs +++ b/Assets/Scripts/Weapons/Grenade.cs @@ -13,24 +13,16 @@ public class Grenade : MonoBehaviour [Tooltip("After how many seconds the explosion Gameobject gets deleted!")] [SerializeField] float lengthOfExplosion = 1; private float countdown; - [Header("Camera Shake Info")] // NOT WOKRING BECAUSE THE CAMERA IS FIXED IN PLACE - [SerializeField] bool cameraShakeActive = true; - [SerializeField] float cameraShakeRadius = 6f; - [SerializeField] float cameraShakeDuration = 1f; - [SerializeField] AnimationCurve cameraShakeCurve; - [Header("Explosion GameObject")] + [SerializeField] float cameraShakeRadius = 6f; + [SerializeField] GameObject explodeParticle; - [Header("Scripts")] [SerializeField] Weapon weapon; [Header("Debug")] [SerializeField] bool showExplosion = true; - // To change it from other scripts - public bool CameraShakeActive { get => cameraShakeActive; set => cameraShakeActive = value; } - void Start() { countdown = timer; } @@ -57,11 +49,7 @@ public class Grenade : MonoBehaviour // Destroys explosion particle after on second Destroy(spawnedExplosion, lengthOfExplosion); } - - if (cameraShakeActive) { - // Coroutine for camera shake to nearby Players - StartCoroutine(cameraShake()); - } + StartCoroutine(cameraShake()); // Coroutine for adding explosion force to nearby objects StartCoroutine(addExplosionForce()); @@ -70,30 +58,20 @@ public class Grenade : MonoBehaviour } IEnumerator cameraShake() { + // Gets all collider that are in a sphere around the grenade Collider[] colliders = Physics.OverlapSphere(transform.position, cameraShakeRadius); - foreach(Collider nearbyObject in colliders){ + // Iterate over all colliders found in radius + foreach (Collider nearbyObject in colliders) { + // Check if nearby object is a Player and if Collider is not a CharacterController (can be changed to CapsuleCollider) if (nearbyObject.GetComponent() && nearbyObject.GetType() != typeof(UnityEngine.CharacterController)) { - // Start coroutine that shakes the camera - StartCoroutine(shaking(nearbyObject)); + // Starts camera shake on player + float distance = Vector3.Distance(transform.position, nearbyObject.transform.position); + nearbyObject.GetComponent().cameraShake(); } } yield return null; } - IEnumerator shaking(Collider obj) { - // Getting neck from player - GameObject neck = obj.GetComponent().PlayerNeck; - Vector3 startPos = neck.transform.position; - float elapsedTime = 0f; - while(elapsedTime < cameraShakeDuration) { - elapsedTime += Time.deltaTime; - float strength = cameraShakeCurve.Evaluate(elapsedTime / cameraShakeDuration); - neck.transform.position = startPos + Random.insideUnitSphere * strength; - } - neck.transform.position = startPos; - yield return null; - } - IEnumerator addExplosionForce() { // Gets all collider that are in a sphere around the grenade Collider[] colliders = Physics.OverlapSphere(transform.position, grenadeRadius);