Ok, so I am trying to just practice using unity so I decided to make myself a TD since it will involve a lot of different objects and code (Tower, enemies, collider, Waypoints, point/money, among others)
Now I have ran into a bit of an issue, I have made the bullet already and that’s fine, it flies towards the enemy block using Navmesh(Probably a better way to do this, but it works)
The only issue I have now is that it will not detect the Enemy when it enters, or it won’t fire which is kind of really annoying, if I put my firecode in the update function it works as intended it’s just the detecting I am having an issue with.
The code I am using in my tower is as follows;
using UnityEngine;
using System.Collections;
public class Tower1Controller : MonoBehaviour {
public float fireRate = 1.0f;
//public int damagePerShot = 20;
//public float timeBetweenBullets = 1.0f;
//BoxCollider boxCollider;
public GameObject bullet;
public Transform bulletSpawn;
//public static float time;
public bool Fire = false;
bool FireTemp = false;
private float nextFire;
private float FireRateStore;
void Start()
{
FireRateStore = fireRate;
}
void OnTriggerEnter (Collider other)
{
if (other.tag == "Tower") //|| other.tag == "Enemy")
{
return;
}
if (other.tag == "Enemy")
{
FireTemp = true;
if(FireTemp == true)
{
Shoot ();
FireTemp = false;
}
}
//gameController.AddScore(scoreValue);
//Destroy (other.gameObject);
}
void OnTriggerStay (Collider other)
{
// if (other.tag == "Tower")
// {
// return;
// }
if (other.tag == "Enemy")
{
if(fireRate> 0.0f)
{
Fire = false;
fireRate -= Time.deltaTime;
}
else
{
Fire = true;
}
if(Fire == true)
{
Shoot ();
}
}
}
void Shoot ()
{
//nextFire = Time.time + fireRate;
Instantiate(bullet, bulletSpawn.position, bulletSpawn.rotation);
fireRate = FireRateStore;
Fire=false;
}
}
I’ve tried to get it to fire for quite some time, so I switched it about from one piece of code to another so don’t worry if it is confusing
Any help would be awesome, thanks.
It randomly fixed itself, no idea why, left it on for a night, came back to it the next day and it had fixed itself when I booted it up.