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

View File

@@ -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()
{

View File

@@ -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);

View File

@@ -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<Player>() && 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<ProcedualAnimationController>().cameraShake();
}
}
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() {
// Gets all collider that are in a sphere around the grenade
Collider[] colliders = Physics.OverlapSphere(transform.position, grenadeRadius);