public void FireBullet(Vector3 pos, float speed) { Debug.Log("FireBullet!"); if (!Reloading) { if (Magazine == 1) { StartReload(); } Vector3 thisPosition = pos; Vector3 targetPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); targetPosition.z = 0; // rotate towards target position Quaternion rotation = Quaternion.FromToRotation(Vector3.up, targetPosition - thisPosition); GameObject goBullet = UnityEngine.Object.Instantiate(currentWeapon.bulletPrefab, thisPosition, rotation); Bullet bullet = goBullet.GetComponent <Bullet>(); Vector3 targetVector = targetPosition - thisPosition; bullet.GetComponent <Rigidbody2D>().AddForce(targetVector.normalized * speed); LastFired = currentWeapon.fireRate; Magazine -= 1; OnMagazineChange?.Invoke(Magazine); } }
public void CheckReload(float time) { if (Reloading) { ReloadTimer -= time; if (ReloadTimer <= 0) { Reloading = false; Magazine = currentWeapon.magazineCapacity; OnReloadChange?.Invoke(Reloading); OnMagazineChange?.Invoke(Magazine); } } }