mirror of
https://github.com/DerTyp7/defrain-shooter-unity.git
synced 2025-10-29 20:52:10 +01:00
Camera Shake, WeaponSwitch Animation
Added camera shake. Its not working.. i dont know why. WeaponSwitch is also not working
This commit is contained in:
@@ -13,16 +13,24 @@ 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] GameObject explodeParticle;
|
||||
|
||||
[Header("Scripts")]
|
||||
[SerializeField] Weapon weapon;
|
||||
[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;
|
||||
}
|
||||
@@ -43,23 +51,58 @@ public class Grenade : MonoBehaviour
|
||||
|
||||
/* - Spawn explosion particles and add force to nearby objects - */
|
||||
private void Explode() {
|
||||
if (showExplosion)
|
||||
{
|
||||
if (showExplosion) {
|
||||
// Spawns explosion particle
|
||||
GameObject spawnedExplosion = Instantiate(explodeParticle, transform.position, transform.rotation);
|
||||
// Destroys explosion particle after on second
|
||||
Destroy(spawnedExplosion, lengthOfExplosion);
|
||||
}
|
||||
|
||||
|
||||
if (cameraShakeActive) {
|
||||
// Coroutine for camera shake to nearby Players
|
||||
StartCoroutine(cameraShake());
|
||||
}
|
||||
// Coroutine for adding explosion force to nearby objects
|
||||
StartCoroutine(addExplosionForce());
|
||||
|
||||
// Destroys grenade
|
||||
Destroy(gameObject);
|
||||
}
|
||||
|
||||
IEnumerator 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));
|
||||
}
|
||||
}
|
||||
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);
|
||||
// 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.transform.gameObject.GetComponent<Player>() && nearbyObject.GetType() != typeof(UnityEngine.CharacterController)) {
|
||||
if (nearbyObject.GetComponent<Player>() && nearbyObject.GetType() != typeof(UnityEngine.CharacterController)) {
|
||||
// Remove health from player
|
||||
nearbyObject.transform.gameObject.GetComponent<Player>().RemoveHealth(weapon.Damage);
|
||||
nearbyObject.GetComponent<Player>().RemoveHealth(weapon.Damage);
|
||||
} else {
|
||||
// Get Rigidbody from nearby object and...
|
||||
Rigidbody rb = nearbyObject.GetComponent<Rigidbody>();
|
||||
@@ -71,7 +114,6 @@ public class Grenade : MonoBehaviour
|
||||
}
|
||||
}
|
||||
hasExploded = true;
|
||||
// Destroys grenade
|
||||
Destroy(gameObject);
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user