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