I made a simple test fps game,i made a ak47 and disabled all of its colliders so it doesnt collide with other objects so it doesnt interact with anything,were gonna need that info later.
I wrote it so you can launch projectiles.
It instantiates a prefab with this script attached to it:
.
.
public class ProjectileScript : MonoBehaviour
{
public GameObject projectileExplosion;
void Start()
{
Invoke("Explode", 20f);
}
void Update()
{
transform.Translate(Vector3.forward * Time.deltaTime);
}
private void OnCollisionEnter(Collision collision)
{
Explode();
}
void Explode()
{
Instantiate(projectileExplosion, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
.
.
So it invokes Explode() at start so it doesnt go on forever.
Then in update it just goes forward.
And on collision it calls Explode(),that simple.
When i fire it,it does go forward.But it just passes thru floor,stairs,everything.Only way its gonna explode is if it touches me or if i let it go for 20s.
This is how i instantiate it in gun script:
.
.
switch (shootsProjectiles)
{
case true:
Instantiate(projectilePrefab, transform.position, transform.rotation);
break;
case false:
break;
}
Thats it,thats all that there is to it.It passes thru ground and everything else,just when i walk into it or i get hit by it it explodes.
Note:
Everything in my scene is untagged and applied default layer.
Pls help.