CameraShake

Camera shake is working now
This commit is contained in:
Noah4ever
2021-12-13 16:02:11 +01:00
parent a2ac91bf6e
commit b2b997307b
7 changed files with 174 additions and 49 deletions

36
Assets/CameraShake.cs Normal file
View File

@@ -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<Player>() && 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;
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: aee84ae112d831740ac546181a8c23e4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -374,6 +374,7 @@ GameObject:
- component: {fileID: 8357986889675898169} - component: {fileID: 8357986889675898169}
- component: {fileID: 701870116503968957} - component: {fileID: 701870116503968957}
- component: {fileID: 7564572533756368624} - component: {fileID: 7564572533756368624}
- component: {fileID: 6541612578984644575}
m_Layer: 0 m_Layer: 0
m_Name: Player m_Name: Player
m_TagString: Player m_TagString: Player
@@ -525,6 +526,33 @@ MonoBehaviour:
AimPoint: {fileID: 2992488853448454767} AimPoint: {fileID: 2992488853448454767}
HoldPoint: {fileID: 8043317747957554736} HoldPoint: {fileID: 8043317747957554736}
isAiming: 0 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: rotationMod:
- {x: 0, y: 0, z: 0, w: 0} - {x: 0, y: 0, z: 0, w: 0}
- {x: 0, y: 0, z: 0, w: 0} - {x: 0, y: 0, z: 0, w: 0}
@@ -572,8 +600,6 @@ MonoBehaviour:
username: username:
usernameTextObj: {fileID: 0} usernameTextObj: {fileID: 0}
health: 0 health: 0
playerNeck: {fileID: 7061967201775281134}
playerCamera: {fileID: 6272346181657429395}
--- !u!114 &1258077278480047560 --- !u!114 &1258077278480047560
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -788,6 +814,55 @@ MonoBehaviour:
syncInterval: 0.1 syncInterval: 0.1
clientAuthority: 1 clientAuthority: 1
animator: {fileID: 1978909962} 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 --- !u!1 &6272346181657429393
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@@ -1016,8 +1091,8 @@ Transform:
m_PrefabInstance: {fileID: 0} m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 7061967201775281134} m_GameObject: {fileID: 7061967201775281134}
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalRotation: {x: -0, y: -0, z: -0, w: 1}
m_LocalPosition: {x: 0, y: 1.66, z: 0} m_LocalPosition: {x: 0, y: 1.6599998, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1} m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0 m_ConstrainProportionsScale: 0
m_Children: m_Children:

View File

@@ -5590,7 +5590,7 @@ PrefabInstance:
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 732033708985862910, guid: bd217f9f85ce78e46a925776562625e8, type: 3} - target: {fileID: 732033708985862910, guid: bd217f9f85ce78e46a925776562625e8, type: 3}
propertyPath: m_LocalPosition.y propertyPath: m_LocalPosition.y
value: 1.347 value: 1.44
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 732033708985862910, guid: bd217f9f85ce78e46a925776562625e8, type: 3} - target: {fileID: 732033708985862910, guid: bd217f9f85ce78e46a925776562625e8, type: 3}
propertyPath: m_LocalPosition.z propertyPath: m_LocalPosition.z
@@ -5718,11 +5718,11 @@ PrefabInstance:
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3}
propertyPath: cameraShakeCurve.m_Curve.Array.data[1].time propertyPath: cameraShakeCurve.m_Curve.Array.data[1].time
value: 0.47748455 value: 0.7053975
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3}
propertyPath: cameraShakeCurve.m_Curve.Array.data[2].time propertyPath: cameraShakeCurve.m_Curve.Array.data[2].time
value: 3.651196 value: 3.7575498
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3}
propertyPath: cameraShakeCurve.m_Curve.Array.data[0].value propertyPath: cameraShakeCurve.m_Curve.Array.data[0].value
@@ -5730,15 +5730,15 @@ PrefabInstance:
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3}
propertyPath: cameraShakeCurve.m_Curve.Array.data[1].value propertyPath: cameraShakeCurve.m_Curve.Array.data[1].value
value: 3.9823725 value: 2.779751
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3}
propertyPath: cameraShakeCurve.m_Curve.Array.data[2].value propertyPath: cameraShakeCurve.m_Curve.Array.data[2].value
value: 1.3784328 value: 0.2894435
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3}
propertyPath: cameraShakeCurve.m_Curve.Array.data[0].inSlope propertyPath: cameraShakeCurve.m_Curve.Array.data[0].inSlope
value: 8.401682 value: 3.9536607
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3}
propertyPath: cameraShakeCurve.m_Curve.Array.data[1].inSlope propertyPath: cameraShakeCurve.m_Curve.Array.data[1].inSlope
@@ -5746,7 +5746,7 @@ PrefabInstance:
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3}
propertyPath: cameraShakeCurve.m_Curve.Array.data[2].inSlope propertyPath: cameraShakeCurve.m_Curve.Array.data[2].inSlope
value: -0.82047147 value: -0.81591856
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3}
propertyPath: cameraShakeCurve.m_Curve.Array.data[0].inWeight propertyPath: cameraShakeCurve.m_Curve.Array.data[0].inWeight
@@ -5754,7 +5754,7 @@ PrefabInstance:
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3}
propertyPath: cameraShakeCurve.m_Curve.Array.data[0].outSlope propertyPath: cameraShakeCurve.m_Curve.Array.data[0].outSlope
value: 8.401682 value: 3.9536607
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3}
propertyPath: cameraShakeCurve.m_Curve.Array.data[1].inWeight propertyPath: cameraShakeCurve.m_Curve.Array.data[1].inWeight
@@ -5770,7 +5770,7 @@ PrefabInstance:
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3}
propertyPath: cameraShakeCurve.m_Curve.Array.data[2].outSlope propertyPath: cameraShakeCurve.m_Curve.Array.data[2].outSlope
value: -0.82047147 value: -0.81591856
objectReference: {fileID: 0} objectReference: {fileID: 0}
- target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3} - target: {fileID: 9082234699677627348, guid: bd217f9f85ce78e46a925776562625e8, type: 3}
propertyPath: cameraShakeCurve.m_Curve.Array.data[0].outWeight propertyPath: cameraShakeCurve.m_Curve.Array.data[0].outWeight

View File

@@ -24,10 +24,6 @@ public class Player : NetworkBehaviour
private int kills; private int kills;
private int deaths; 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() private void Start()
{ {

View File

@@ -101,12 +101,41 @@ public class ProcedualAnimationController : NetworkBehaviour
[SerializeField] GameObject HoldPoint; [SerializeField] GameObject HoldPoint;
public bool isAiming = false; 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]; Vector3[] positionMod = new Vector3[4];
public Quaternion[] rotationMod = new Quaternion[4]; public Quaternion[] rotationMod = new Quaternion[4];
public Transform GunRightHandREF { get => gunRightHandREF; set => gunRightHandREF = value; } public Transform GunRightHandREF { get => gunRightHandREF; set => gunRightHandREF = value; }
public Transform GunLeftHandREF { get => gunLeftHandREF; set => gunLeftHandREF = 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() public void walkAnimation()
{ {
playerAnimator.SetFloat("x", playerController.localVelocity.x / playerController.currentMaxSpeed); playerAnimator.SetFloat("x", playerController.localVelocity.x / playerController.currentMaxSpeed);

View File

@@ -13,24 +13,16 @@ public class Grenade : MonoBehaviour
[Tooltip("After how many seconds the explosion Gameobject gets deleted!")] [Tooltip("After how many seconds the explosion Gameobject gets deleted!")]
[SerializeField] float lengthOfExplosion = 1; [SerializeField] float lengthOfExplosion = 1;
private float countdown; 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; [SerializeField] GameObject explodeParticle;
[Header("Scripts")]
[SerializeField] Weapon weapon; [SerializeField] Weapon weapon;
[Header("Debug")] [Header("Debug")]
[SerializeField] bool showExplosion = true; [SerializeField] bool showExplosion = true;
// To change it from other scripts
public bool CameraShakeActive { get => cameraShakeActive; set => cameraShakeActive = value; }
void Start() { void Start() {
countdown = timer; countdown = timer;
} }
@@ -57,11 +49,7 @@ public class Grenade : MonoBehaviour
// Destroys explosion particle after on second // Destroys explosion particle after on second
Destroy(spawnedExplosion, lengthOfExplosion); Destroy(spawnedExplosion, lengthOfExplosion);
} }
if (cameraShakeActive) {
// Coroutine for camera shake to nearby Players
StartCoroutine(cameraShake()); StartCoroutine(cameraShake());
}
// Coroutine for adding explosion force to nearby objects // Coroutine for adding explosion force to nearby objects
StartCoroutine(addExplosionForce()); StartCoroutine(addExplosionForce());
@@ -70,30 +58,20 @@ public class Grenade : MonoBehaviour
} }
IEnumerator cameraShake() { IEnumerator cameraShake() {
// Gets all collider that are in a sphere around the grenade
Collider[] colliders = Physics.OverlapSphere(transform.position, cameraShakeRadius); Collider[] colliders = Physics.OverlapSphere(transform.position, cameraShakeRadius);
// Iterate over all colliders found in radius
foreach (Collider nearbyObject in colliders) { 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<Player>() && nearbyObject.GetType() != typeof(UnityEngine.CharacterController)) { if (nearbyObject.GetComponent<Player>() && nearbyObject.GetType() != typeof(UnityEngine.CharacterController)) {
// Start coroutine that shakes the camera // Starts camera shake on player
StartCoroutine(shaking(nearbyObject)); float distance = Vector3.Distance(transform.position, nearbyObject.transform.position);
nearbyObject.GetComponent<ProcedualAnimationController>().cameraShake();
} }
} }
yield return null; yield return null;
} }
IEnumerator shaking(Collider obj) {
// Getting neck from player
GameObject neck = obj.GetComponent<Player>().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() { IEnumerator addExplosionForce() {
// Gets all collider that are in a sphere around the grenade // Gets all collider that are in a sphere around the grenade
Collider[] colliders = Physics.OverlapSphere(transform.position, grenadeRadius); Collider[] colliders = Physics.OverlapSphere(transform.position, grenadeRadius);