- Home /
Guided Missiles help?
How to destroy a missile after it explodes?
Answer by N1warhead · Oct 03, 2014 at 09:32 PM
This is how you making a Missle Instantiate and find an object(s) with a certain tag! It is in C#. You may have to change it to suit your needs better, but it does work! Attach this script TO YOUR PREFAB THAT GETS INSTANTIATED!
Transform Target;
float Speed = 20f;
float MaxDistance = 50f;
float DieTime = 1.5f; // Destroyed in 1.5 seconds (Change to
// Change to your needs!)
void Start(){
DieTime = Time.time;
Target = GameObject.FindGameObjectWithTag ("Player").transform;
}
// Update is called once per frame
void Update ()
{
transform.LookAt (Target);
AI ();
if (Time.time - DieTime > 1.5f) {
Destroy (this.gameObject);
Debug.Log ("DESTROYED BY TIME");
Destroy (this.gameObject);
}
if(Target == null){
Target = GameObject.Find ("Player").transform;
}
}
void AI()
{
if (Vector3.Distance (transform.position, Target.position) <= MaxDistance) {
transform.position += transform.forward * Speed * Time.deltaTime;
}
}
void OnDestroy(){
if (Target) {
Destroy (gameObject);
Debug.Log ("Missile Destroyed");
}
}
}
As said, you may have to change things up for your needs, but this DOES WORK! in C#.
Hope this answered your question properly! If so please mark as answered.
N1warhead thx. I have updated my question because i had changed it before. Jamster is right about his comment. Newbie mistake
You're very welcome buddy.
Don't feel bad, yesterday I sat around trying to figure something out and I made the most rookie mistake ever...
We all go through it, just takes patience and dedication. Some stuff is easy some stuff is hard, but if you really think about it, most stuff you do for just an ordinary game is all explanatory with the basics we just skip past the basics because we think it's not basic, when in reality it is, but it isn't if you don't know how to get an out of the box view of the problems we face.
I'm glad I was able to help you. If you ever have any questions, feel free to message me if you'd like. I don't know everything, but more pairs of eyes is better than one pair.
Answer by Jamster · Nov 25, 2013 at 10:37 PM
Destroy(gameObject) will destroy the game object the script is attached to, if you're using OnCollisionEnter() on the missile try this instead:
function OnCollisionEnter(col : Collision){
Destroy(col.gameObject);
}
That will destroy the object it hits instead. If you wanted an explosion etc to appear first it must be called before the Destroy otherwise it wount be read!
O$$anonymous$$, you've changed your question so (aside from my answer not making sense now) you seem to be asking some thing different. How do you mean "add force to a missile before it explodes"? Do you mean the missile or do you mean the thing it hits? Have you tried using rigidbody.AddForce or just using Transfor.Translate?
After more than a year, i see my old questions "i say wow it was a big struggle to learn program$$anonymous$$g and other necassary 3d work.". Even after a year, there is still so much things to learn.
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Trash Collection For Instantiated Game Object 1 Answer
How to instantiate a prefab after its been destroyed 1 Answer
Instantiating prefabs: "The object of type GameObject has been destroyed". 1 Answer
Instantiate problem 1 Answer