public void ApplyKnockback() { #if UNITY_EDITOR // For editor debugging, select whether or not player should have knockback applied to them from weapon fire. if (!applyWeaponKnockback) { return; } #endif // If currently selected item is null, dont do anything if (pHand.CurrentlySelected == null) { return; } // Knockback currently is only applied from using weapon cards. (subject to change) if (pHand.CurrentlySelected.CardType != ItemType.weapon) { return; } // Type cast so I can extract the Knockback data from the weapon WeaponCardSO weapon = pHand.CurrentlySelected as WeaponCardSO; // Direction the knockback should be applied (should be opposite of direction projectile was fired) Vector3 dir = -(_projSpawnLocation.transform.position - transform.position); // I don't think I need to check if null anymore but its here just in case. // Also, knockback is just an impulse force applied to player's rigidbody2d if (weapon != null) { _rb.AddForce(weapon.KnockBackMagnitude * dir.normalized * Time.fixedDeltaTime, ForceMode2D.Impulse); } }
/* --------------------------------------- */ /* ---------------- Public --------------- */ /* --------------------------------------- */ /// <summary> Only used for shooting weapon cards. </summary> public void FireProjectile() { // Cant shoot if player not holding a gun if (pHand.CurrentlySelected == null) { return; } // If currently selected card is not of type "weapon" don't do anything if (pHand.CurrentlySelected.CardType != ItemType.weapon) { return; } // Type conversion to WeaponCardSO since not all cards can shoot... WeaponCardSO weaponCardRefr = pHand.CurrentlySelected as WeaponCardSO; /* * Might modify how projectile timing works. This currently allows for "full auto" weapon types * without instantiating like hundreds of projectiles in a fraction of a second since this function * could theoretically be called every single frame */ if ((Time.time - timeSinceLastAttack) >= weaponCardRefr.ROF) { // Cache time last fired weapon timeSinceLastAttack = Time.time; // Spawn the projectile to fire ProjectileHandler refr = LeanPool.Spawn <ProjectileHandler>(_projComponentRefr, _projSpawnLocation.position, _projSpawnLocation.rotation, _projParentObject.transform); // Set the ProjectileData for the spawned projectile refr._projData = weaponCardRefr.ProjectileData; if (UpdateProjectileData != null) { UpdateProjectileData.RaiseEvent(refr.gameObject.GetInstanceID()); } // Activate necessary events if (KnockbackEvent != null) { KnockbackEvent.RaiseEvent(); } if (CameraShakeEvent != null) { CameraShakeEvent.RaiseEvent(weaponCardRefr._camShakePreset); } } }