- Home /
Tank Missle Colide then explode and destroy
Well I have been trying to do this for few hours now... What I want to happen is I want the missile a sphere in the video when it collides with something I want to to explode using the simple detonator from the detonator prefab provided by unity. After that I want it to "destroy"... This is as far as I got but of course it did not work. I am very confused on how I have to set this up... Video and the code is below.
http://forum.unity3d.com/threads/120389-Tank-Game-Scripting-Fire
The code that was in the video var projectilPrefab : Transform; var powerOfShoot : float = 3000;
function Update () { if(Input.GetButtonDown("Jump")){ audio.Play();
var projectil = Instantiate(projectilPrefab, transform.position, transform.rotation);
projectil.rigidbody.AddForce(transform.forward * powerOfShoot);
}
}
The code that I tried but did not succeed var projectilPrefab : Transform; var explosionPrefab : Transform; var projectildes : Transform; var powerOfShoot : float = 3000;
function Update () { if(Input.GetButtonDown("Jump")){ audio.Play();
var projectil = Instantiate(projectilPrefab, transform.position, transform.rotation);
projectil.rigidbody.AddForce(transform.forward * powerOfShoot);
}
} function OnCollisionEnter(collision : Collision) { // Rotate the object so that the y-axis faces along the normal of the surface var contact : ContactPoint = collision.contacts[0]; var rot : Quaternion = Quaternion.FromToRotation(Vector3.up, contact.normal); var pos : Vector3 = contact.point; Instantiate(explosionPrefab, pos, rot); // Destroy the projectile Destroy (projectildes); }
Answer by Lo0NuhtiK · Jan 25, 2012 at 06:46 AM
Looks like you need two scripts to me. One on-gun for shooting , one on-projectile for collision.
var projectile : Rigidbody ;
var powerOfShoot : float = 3000 ;
function Update(){
if(Input.GetButtonDown("Jump")){
audio.Play() ;
var clone : Rigidbody ;
clone = Instantiate(projectile, transform.positon, transform.rotation) ;
clone.rigidbody.AddForce(transform.forward * powerOfShoot) ;
}
}
var explosion : Transform ;
function OnCollisionEnter(other : Collision){
var contact : ContactPoint = other.contacts[0] ;
var rot : Quaternion = Quaternion.FromToRotation(Vector3.up, contact.normal) ;
var cloneExplode : Transform ;
cloneExplode = Instantiate(explosion, contact.point, rot) ;
Destroy(gameObject) ;
}
Where it says " Destroy(gameObject) ;" do i replace gameObject with somthing?
@kmgilbert100 come-on man you have no knowledge of functions and objects in unity. don't just cram up everything and find scripts here and there. you need to learn things yourself and learn the basics first.
Your answer
Follow this Question
Related Questions
Shuriken Explosion Example - Collision 0 Answers
Unity Detonator - Detect Shockwave on Object 0 Answers
Objects do not destroy on Collision 1 Answer
Explosion Elimination script 2 Answers