2023-06-19 01:52:18 +02:00
|
|
|
using System.Collections;
|
|
|
|
using UnityEngine;
|
2023-06-19 10:55:03 +02:00
|
|
|
using UnityEngine.Serialization;
|
2023-06-19 01:52:18 +02:00
|
|
|
|
|
|
|
public class KitchenGun : MonoBehaviour
|
|
|
|
{
|
|
|
|
// The shot projectiles
|
|
|
|
public GameObject projectilePrefab;
|
|
|
|
// Time between shots, game needs to be restarted to take effect
|
2023-06-19 10:55:03 +02:00
|
|
|
public float shotIntervalSeconds = 1f;
|
|
|
|
// Time alive for the projectiles
|
|
|
|
public float shotAliveSeconds = 1f;
|
2023-06-19 11:03:08 +02:00
|
|
|
// Time until collider gets enabled
|
|
|
|
public float colliderDisabledSeconds = .2f;
|
2023-06-19 01:52:18 +02:00
|
|
|
// Speed of projectiles
|
|
|
|
public Vector2 shotSpeed = new Vector2(1f, 0f);
|
|
|
|
// Smallest step size for shot speed
|
|
|
|
private short shotSpeedStepSize = 50;
|
|
|
|
|
|
|
|
// Start is called before the first frame update
|
|
|
|
void Start()
|
|
|
|
{
|
2023-06-19 10:55:03 +02:00
|
|
|
InvokeRepeating("RecurringBang",0f,shotIntervalSeconds);
|
2023-06-19 01:52:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void RecurringBang()
|
|
|
|
{
|
|
|
|
StartCoroutine("Bang");
|
|
|
|
}
|
|
|
|
|
|
|
|
private IEnumerator Bang()
|
|
|
|
{
|
|
|
|
Transform t = transform;
|
|
|
|
Vector3 position = t.position;
|
|
|
|
|
|
|
|
// create projectile
|
|
|
|
GameObject projectile = Instantiate(projectilePrefab,
|
|
|
|
position,
|
|
|
|
t.rotation);
|
2023-06-19 11:03:08 +02:00
|
|
|
|
2023-06-19 01:52:18 +02:00
|
|
|
// determine correct force vector
|
|
|
|
Vector2 force = shotSpeed;
|
|
|
|
if (t.localScale.x < 0) {
|
|
|
|
force *= -1;
|
|
|
|
}
|
|
|
|
force *= shotSpeedStepSize;
|
|
|
|
|
|
|
|
// add force to projectile
|
2023-06-19 11:03:08 +02:00
|
|
|
projectile.GetComponent<Rigidbody2D>().AddRelativeForce(force);
|
|
|
|
|
|
|
|
// wait until enabling collider
|
|
|
|
yield return new WaitForSeconds(colliderDisabledSeconds);
|
|
|
|
projectile.GetComponent<Collider2D>().enabled = true;
|
2023-06-19 01:52:18 +02:00
|
|
|
|
2023-06-19 10:55:03 +02:00
|
|
|
// wait given time before destroying
|
|
|
|
yield return new WaitForSeconds(shotAliveSeconds);
|
2023-06-19 01:52:18 +02:00
|
|
|
|
|
|
|
Destroy(projectile);
|
|
|
|
}
|
|
|
|
}
|