Time alive der Projektile und Schussinterval entkoppelt

This commit is contained in:
Maximilian Wagner 2023-06-19 10:55:03 +02:00
parent 91d44a71d6
commit 294b920f8a

View File

@ -1,12 +1,15 @@
using System.Collections;
using UnityEngine;
using UnityEngine.Serialization;
public class KitchenGun : MonoBehaviour
{
// The shot projectiles
public GameObject projectilePrefab;
// Time between shots, game needs to be restarted to take effect
public float shotInterval = 1f;
public float shotIntervalSeconds = 1f;
// Time alive for the projectiles
public float shotAliveSeconds = 1f;
// Speed of projectiles
public Vector2 shotSpeed = new Vector2(1f, 0f);
// Smallest step size for shot speed
@ -15,7 +18,7 @@ public class KitchenGun : MonoBehaviour
// Start is called before the first frame update
void Start()
{
InvokeRepeating("RecurringBang",0f,shotInterval);
InvokeRepeating("RecurringBang",0f,shotIntervalSeconds);
}
// Update is called once per frame
@ -43,15 +46,14 @@ public class KitchenGun : MonoBehaviour
if (t.localScale.x < 0) {
force *= -1;
}
force *= shotSpeedStepSize;
// add force to projectile
Rigidbody2D pBody = projectile.GetComponent<Rigidbody2D>();
pBody.AddRelativeForce(force);
yield return new WaitForSeconds(shotInterval);
Debug.Log("Bang!");
// wait given time before destroying
yield return new WaitForSeconds(shotAliveSeconds);
Destroy(projectile);
}