- Home /
How to add explosion after shot
public var lifeTime : int = 1;
function OnTriggerEnter () {
Destroy(gameObject, lifeTime);
}
Hi guys, I created a 1st person controller with a gun, and its shooting and working fine, and killing my enemies but I want to end with an explosion after they are killed. So, I dont know exactly how to put this down in the script. Above you see my first version and after this text, my second version, it doesnt give too much error but it just isnt working... please help me.
public var lifeTime : int = 1;
var explosion : Transform;
function OnTriggerEnter () {
Destroy(gameObject, lifeTime);
}
function OnTriggerEnter(hit : Collider)
{
if(hit.gameObject == "Bulletspawn")
{
Destroy(hit.gameObject);
var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
Try changing explosion from transfOrm to a game object
I don't think that matters. Instantiate()
requires a Object
as the parameter, not GameObject
.
http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html
Destroyed Bullet:
public var lifeTime : int = 1.5;
function Awake () {
Destroy(gameObject, lifeTime) ;
}
Damage Script:
var damage = 10;
//instant kill
function OnCollisionEnter ( collisionInfo ) {
collisionInfo.other.Send$$anonymous$$essage("ApplyDamage", damage, Send$$anonymous$$essageOptions.DontRequireReceiver) ;
}
Turret Script:
public var BulletSpeed : float = 6000;
public var BulletPrefab : Transform;
var LookAtTarget : Transform;
var nextShotTime : float = 0.0;
var timeBetweenShots : float = 2.0;
var damp : float = 6.0;
var gunShot : AudioClip;
function Update ()
{
if(LookAtTarget)
{
var rotate = Quaternion.LookRotation(LookAtTarget.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotate, Time.deltaTime * damp);
if(nextShotTime <= Time.time)
{
Shoot();
nextShotTime = Time.time + timeBetweenShots;
}
}
}
function Shoot(){
var Bullet = Instantiate(BulletPrefab, transform.Find("TurretBulletSpawn").position, transform.Find("TurretBulletSpawn").rotation);
Bullet.rigidbody.AddForce(transform.forward * BulletSpeed);
audio.PlayOneShot(gunShot);
}
Answer by Chronos-L · Apr 18, 2013 at 02:01 AM
it doesnt give too much error, this makes me worries a little. Do you have any errors at all? Are those warnings and you take them as errors? Or you just trying to say that there are no errors at all?
From what I can see here, the only problem is this line:
//you can't compare a gameobject with string
if(hit.gameObject == "Bulletspawn")
//Correction
//Either this (less likely)
if( hit.gameObject.name == "Bulletspawn" )
//or this (more likely)
if( hit.gameObject.CompareTag("Bulletspawn") )
One more thing, from this documentation, will function OnTriggerEnter ()
work?
@Chronos-L I am not really much into scripting yet.. but I am working on it. $$anonymous$$y FPC shoots the Turret and its killed, so thats fine, it works with this code:
public var lifeTime : int = 1; function OnTriggerEnter () { Destroy(gameObject, lifeTime); }
@Chronos-L But I want to let it explode, not just dissapear, because that just looks lame somehow..
Does fixing the if( hit.gameObject == "BulletSpawn" )
fix the problem? If not, for your second script, try keeping the function OnTriggerEnter( hit : Collider )
and remove the function OnTriggerEnter()
.
@Chronos-L I am not sure what you mean, I already tried what you wrote above and nothing seems to happen. What do I have to do with "if" and what do I have to try with function OnTriggerEnter? If I remove it it gives a huge error..:(
From your code, you will instantiate a explosion if the condition is right. So the problem now should be your setup.
Is this script attached to a enemy or a bullet? And what errors you are getting? What does your current script look like?
Answer by AlucardJay · Apr 19, 2013 at 05:37 PM
Why do you have 2 trigger event functions on your turret? Consider the logic, try to read the code as the machine reads it.
There is a command called Invoke : http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.Invoke.html
With that you can call a function after a set amount of time :
public var lifeTime : int = 1;
var explosion : Transform;
var explosionSound : AudioClip;
function OnTriggerEnter(hit : Collider)
{
if(hit.gameObject == "Bulletspawn")
{
var exp = Instantiate(explosion, gameObject.transform.position, Quaternion.identity);
Invoke( "ExplodeMe", lifeTime );
}
}
function ExplodeMe () {
audio.PlayOneShot(explosionSound);
Destroy(gameObject);
}
@alucardj Hey, thanks for all your help, and sorry for my late reply. And indeed there was some misunderstanding. Also sorry for that, and cheers ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220613100527im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Deform mesh/terrain 0 Answers
Bullets Shot Stay Still 1 Answer
shot delay in between bullets 1 Answer
Collision with terrain 0 Answers
Bullets won't fire? 0 Answers