I have my flak canon firing and then a second later the flak explosion hits the air
sure i think i probably did it very clumsily but it works.
i have the canon shotting at the player above a certain altitude. And it works great but the only problem is:
when the player is diving, the canon will shott, but then no explosion because the player has obviously dived below the firing threshold.
Any ideas welcomed on how to fix this problem.
heres da code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FlakCanonController: MonoBehaviour
{
public Transform target;
private Rigidbody2D rb;
public float rotateSpeed = 200f;
public GameObject canonFires;
public GameObject flakPrefab;
public GameObject bulletSpawnObject;
private bool isAlive = true;
public float FlakSpeed = 0;
private bool shotWasFired = false;
private bool canShoot = true;
private bool flakExplodes = false;
private void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
Vector2 direction = (Vector2)target.position - rb.position;
direction.Normalize();
float rotateAmount = Vector3.Cross(direction, transform.up).z;
rb.angularVelocity = -rotateAmount * rotateSpeed;
if (target.position.y > 1 && isAlive)
{
if (canShoot)
{
FindObjectOfType<AudioManager>().Play("FlakShooting");
StartCoroutine(TimeDelay());
canShoot = false;
StartCoroutine(TimeDelay2());
}
if (flakExplodes)
{
GameObject newFlakExplosion = Instantiate(flakPrefab, bulletSpawnObject.transform.position, transform.rotation);
newFlakExplosion.GetComponent<Rigidbody2D>().AddRelativeForce(Vector2.up * FlakSpeed);
Destroy(newFlakExplosion, 1.0F);
FindObjectOfType<AudioManager>().Play("FlakExplode");
flakExplodes = false;
}
}
}
IEnumerator TimeDelay()
{
yield return new WaitForSeconds(3);
canShoot = true;
}
IEnumerator TimeDelay2()
{
yield return new WaitForSeconds(1);
flakExplodes = true;
}
}
}*/