Shooting FireBalls

Hello,
i’ve got a Problem with my Fireball system.After i’ve shot them(they fly straightforward but not perfectly in the Direction in which i look) they just fly around and hit against some stuff(they rebound) but i want them to explode or disappear after they hit something or even do damage on enemys.I’ve made an FireBall and a SpawnPoint for it.Sorry for my bad English.Thank you :slight_smile:

ShootFireBall Script:

var ShootFireBallPrefab:Transform;

function Update()
{
if(Input.GetMouseButtonDown(0))

{
var ShootFireBall = Instantiate(ShootFireBallPrefab, GameObject.Find(“SpawnPoint”).transform.position, Quaternion.identity);

ShootFireBall.rigidbody.AddForce(transform.forward * 2000);
}
}

Use the “Destroy();” or “Destroy(other GameObject)”. Check in the scripting reference

Thanks for the quick Reply but i couldnt make it :confused: can you do it Pls? the fireball is called FireBall

Hi there,

You need to use Destroy(), yes, but there are other steps involved.

First, the reason why your fireball is going in weird directions is because the AddForce() call you’re using is using the wrong transform’s forward property. I assume you would want to use the “SpawnPoint”'s transform. Make sure the spawn point’s Z axis is pointing in the direction you want the fireball to shoot from, then change this line:

ShootFireBall.rigidbody.AddForce(transform.forward * 2000);

to

ShootFireBall.rigidbody.AddForce(GameObject.Find(“SpawnPoint”).transform.forward * 2000);

As for the collision, you’ll need to add a different script to your fireball prefab, then listen for OnCollisionEnter / OnCollisionStay / OnTriggerEnter / OnTriggerStay depending on how you want your colliders on your objects to be set up. You’ll probably want to try OnCollisionEnter, so look at this page since it has an example of how to destroy your fireball when it hits another object:

Hope this helps, let me know if you get stuck