Hello guys, i’ve got a problem with my game. Here’s the (strange) scenario: enemies have got a box collider, but bullets MUST NOT have a collider. Bullets are slow, so they are visible when i press space to shoot. I want that the bullet will destroy if it collides with an enemy. I have done every function to make it work (like pooling the bullet object and deactivate it after x seconds), but I’ve got a problem with the raycast. I’ve got this attached to the bullet prefab:
void OnEnable ()
{
rayInitialPos = new Ray (bulletSpawnPoint.transform.position, bulletSpawnPoint.transform.forward);
rayEndPos = gameObject.transform.position.z;
if (Physics.Raycast (rayInitialPos, out hit,rayEndPos)) {
if (hit.collider.tag == "Enemy") {
GameObject enemy = hit.collider.gameObject;
gameObject.SetActive (false);
//faccio i controlli per diminuire vita o distruggere oggetto enemy se morto
EnemyScript.TakeDamage (enemy);
}
}
else {
Invoke ("Destroy", 4f);
}
}
void Destroy(){
gameObject.SetActive(false);
}
The fact is that if Physics.Raycast (rayInitialPos, out hit,rayEndPos) it’s true (so i’m in front of an enemy and i’m aiming it), the bullet is set immediately to False and I cannot see the bullet. My idea was to have the rayEndPos value exactly like the length of the bullet, and when the bullet hits the enemy, it disappear. How can I achieve that? Remember that I cannot use OnCollisionEnter and I cannot put a collider inside the bullet object for personal reasons. Thank you in advance!