- Home /
Using raycast for visible bullet collision (not oncollisionenter) in C#
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!
Answer by PizzaPie · Feb 09, 2017 at 05:37 PM
The ray cast should be casted every frame then, and not only on the first one as you have it, put it on the Update() . Also i would change the raycast to physics.Raycast(gameObject.transform.position, gameObject.transform.position.forward, someRange, out hit) change the someRange to achieve the desired effect. May i ask why bullets MUST NOT have colliders? Constant raycasting will have a greater performance cost. Cheers