mirror of
https://github.com/DerTyp7/defrain-shooter-unity.git
synced 2025-10-29 12:52:07 +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:
@@ -24,26 +24,19 @@ public class DebugCanvas : MonoBehaviour
|
||||
}
|
||||
private void Update()
|
||||
{
|
||||
if(Player == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
if(Player == null) {
|
||||
try {
|
||||
Player = GameObject.FindGameObjectWithTag("Player").gameObject;
|
||||
shoot = Player.GetComponent<Shoot>();
|
||||
Debug.Log("Player Found");
|
||||
}
|
||||
catch
|
||||
{
|
||||
catch {
|
||||
//Debug.Log("DEBUG CANVAS PLAYER NOT YET FOUND");
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
DebugTextGrounded.text = "isGrounded: " + Player.GetComponent<PlayerController>().isGrounded.ToString();
|
||||
if (Player)
|
||||
{
|
||||
DebugAmmunition.text = shoot.CurAmmo + " / " + shoot.TotalAmmo;
|
||||
if (Player) {
|
||||
DebugAmmunition.text = shoot.CurAmmo.ToString() + " / " + shoot.TotalAmmo.ToString();
|
||||
}
|
||||
deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
|
||||
float fps = 1.0f / deltaTime;
|
||||
|
||||
@@ -8,14 +8,11 @@ public class Player : NetworkBehaviour
|
||||
public bool isAlive = true;
|
||||
public Team team;
|
||||
|
||||
|
||||
|
||||
[SerializeField]PlayerUIController playerUIController;
|
||||
[SerializeField] PlayerUIController playerUIController;
|
||||
[SerializeField] private const int defaultHp = 100;
|
||||
GameObject GameManager;
|
||||
GameMaster gameMaster;
|
||||
|
||||
|
||||
public ulong clientId;
|
||||
|
||||
[SyncVar(hook = nameof(SetName))]
|
||||
@@ -23,10 +20,15 @@ public class Player : NetworkBehaviour
|
||||
|
||||
[SerializeField] GameObject usernameTextObj;
|
||||
|
||||
[SerializeField] [SyncVar]public int health = 100;
|
||||
[SerializeField] [SyncVar] public int health = 100;
|
||||
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()
|
||||
{
|
||||
|
||||
|
||||
@@ -91,6 +91,8 @@ public class ProcedualAnimationController : NetworkBehaviour
|
||||
float zVelocity = 0f;
|
||||
|
||||
int recoilCounter = 0;
|
||||
[Header("Switch Settings")]
|
||||
[Range(0, 1)] public float switchVal = 0;
|
||||
|
||||
[Header("Aiming Settings")]
|
||||
[SerializeField] float aimSpeed = 0.01f;
|
||||
@@ -99,8 +101,8 @@ public class ProcedualAnimationController : NetworkBehaviour
|
||||
[SerializeField] GameObject HoldPoint;
|
||||
public bool isAiming = false;
|
||||
|
||||
Vector3[] positionMod = new Vector3[3];
|
||||
public Quaternion[] rotationMod = new Quaternion[3];
|
||||
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; }
|
||||
@@ -185,8 +187,8 @@ public class ProcedualAnimationController : NetworkBehaviour
|
||||
{
|
||||
if (isServer)
|
||||
{
|
||||
positionMod = new Vector3[3];
|
||||
rotationMod = new Quaternion[3];
|
||||
positionMod = new Vector3[4];
|
||||
rotationMod = new Quaternion[4];
|
||||
/*-----Recoil-----*/
|
||||
calcRecoilOffset();
|
||||
/*-----Position Recoil-----*/
|
||||
@@ -247,6 +249,21 @@ public class ProcedualAnimationController : NetworkBehaviour
|
||||
aimVal = gravityValue(aimVal, aimSpeed, 1, 0, isAiming);
|
||||
positionMod[2] = Vector3.Lerp(HoldPoint.transform.localPosition, AimPoint.transform.localPosition, Mathf.Pow(aimVal, 1.3f));
|
||||
}
|
||||
|
||||
public void changePistole(bool isSwitching) { // Moves hands doooown ;) and up again NOT WORKING
|
||||
//aimVal = gravityValue(aimVal, aimSpeed, 1, 0, isSwitching);
|
||||
Vector3 b = new Vector3(HoldPoint.transform.localPosition.x, 1, HoldPoint.transform.localPosition.z);
|
||||
//Debug.Log("HALLO: " + positionMod[3]);
|
||||
positionMod[3] = Vector3.Lerp(HoldPoint.transform.localPosition, b, 0.5f);
|
||||
//Debug.Log("HALLO: " + positionMod[3]);
|
||||
}
|
||||
public void changeRifle(bool isSwitching) { // Moves hands up and doooown again ;)
|
||||
//aimVal = gravityValue(aimVal, aimSpeed, 1, 0, isSwitching);
|
||||
Vector3 b = new Vector3(HoldPoint.transform.localPosition.x, HoldPoint.transform.localPosition.y + 1, HoldPoint.transform.localPosition.z);
|
||||
positionMod[3] = Vector3.Lerp(HoldPoint.transform.localPosition, b, 0);
|
||||
//Debug.Log("HALLO2");
|
||||
}
|
||||
|
||||
void calcRecoilOffset()
|
||||
{
|
||||
for (int i = 0; i < recoilCounter; i++)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@ public class WeaponManager : NetworkBehaviour
|
||||
[SerializeField] Camera cam;
|
||||
|
||||
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
procedualAnimationController = GetComponent<ProcedualAnimationController>();
|
||||
@@ -48,24 +47,28 @@ public class WeaponManager : NetworkBehaviour
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public bool switchWeapon(int direction) {
|
||||
// Get next active weapon index
|
||||
int nextActive = searchForNext(activeWeapons, lastWeaponIndex, direction);
|
||||
|
||||
currentWeaponIndex = nextActive;
|
||||
|
||||
procedualAnimationController.OnSwitchWeapon(activeWeapons[currentWeaponIndex]);
|
||||
shoot.setWeapon(activeWeapons[currentWeaponIndex]);
|
||||
Weapon weaponData = activeWeapons[currentWeaponIndex].GetComponent<Weapon>();
|
||||
procedualAnimationController.GunRightHandREF = weaponData.GunRightREF;
|
||||
procedualAnimationController.GunLeftHandREF = weaponData.GunLeftREF;
|
||||
// play weapon switch animation
|
||||
switchAnimation(weaponData.WeaponKind.ToString()); // play weapon switch animation
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void switchAnimation(string weaponType) {
|
||||
switch (weaponType) {
|
||||
case "Rifle": procedualAnimationController.changeRifle(true); break;
|
||||
case "Pistole": procedualAnimationController.changePistole(true); break;
|
||||
case "Knife": ; procedualAnimationController.changePistole(true); break;
|
||||
case "Grenade": ; procedualAnimationController.changePistole(true); break;
|
||||
}
|
||||
}
|
||||
private int searchForNext(List<GameObject> l, int lastActive = 0, int direction = 1)
|
||||
{
|
||||
int current = lastActive + direction;
|
||||
|
||||
Reference in New Issue
Block a user