- Home /
On-Click shoot missile and explode where clicked....
How can I setup a On-click shoot missile and explode where you clicked?
I know how to do most of it but I'm wondering how to make it explode when it reaches where you click not when it collides with other things.
My Code:
if (GUI.Button (Rect (0,0,100,100), "SHOOT")) { var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
// Give it an initial forward velocity. The direction is along the z-axis of // the missile launcher's transform. instantiatedProjectile.velocity = transform.TransformDirection( Vector3 (0, 0, initialSpeed)); // Ignore collisions between the missile and the character controller Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider); }
Thanks.
Answer by Tetrad · Jul 12, 2010 at 08:07 PM
How you're firing and moving the missile is going to determine how you cause it to explode.
If you only want it to explode when it hits where you clicked, you wouldn't bother with collision at all. You'd just set up your script with a target point (so you have somewhere in 3D where you "clicked" that you want your missile to go to), and you just set up your movement/explosion script such that when the target is at or behind where your missile is (assuming you're going straight towards it), you blow up your missile.
If you post the script you're currently using for your missiles you might get some pointers on what to change. I'm not going to write one for you though.
Added script for my shooting...dunno how to explode when reaches where I clicked.
Well your first problem is that you need to figure out "where you clicked". Right now you're clicking on a button, not the world. Generally speaking you would use some raycast method to figure out the point in the world where you clicked. There are lots of questions on that point. From there you can figure out the direction you need to go by subtracting the start position from the target position. And you can do distance checks every frame to figure out when you hit your target point.
Answer by Kashaunzilla · May 09, 2011 at 09:20 PM
Well if your are trying use a GUI button thing and when you click the button you are able to shoot missles will be a little more complicated. But you can just use a button. Plus the game will look more realistic if it explodes when ever it hits something. So here is my script on how it works.
// This variable is the missle which requires a rigidbody to be attached to it so it can move around. var missle = Rigidbody;
//What makes the update your should know this but to be on the safe side. function Update () { // the if statement is to see if you clicked the button(key), the m stands for missle in the if statement if ( Input.GetKeyDown ("m") ) { //this is var shooting the missle from where you want the missle to come from. So create a gameObject and call is MissleSpawn and place it under the player so it can be fallowed, and if its mutliplayer the transform part will only search for it if you shoot it and no one elses. var missle = Instantiate(missle, transform.Find("MissleSpawn").transform.position, Quaternion.identity);
//This is so it can move. If you dont add this it will just fall in the place, and kill your
Rigidbody.AddForce(forward * 600);
Now it will stay the same forward speed until it collides with something.
}
}
If there is any errors let me know and i can fix them so that way it will work. But if you want it to be in a Gui a way then you need two scripts because one for Gui and the other for the missle luanching, ok here is the Gui one. With how it works.
//Searchs for a gameObject with the missleSpawn on it to call for it
private var MissleSpawn : missleSpawn;
//using the private var function Start () { MissleSpawn = GetComponent(missleSpawn); }
//calls for the GUI
function OnGUI ()
{
//makes the GUI button for you at the top left part of your screen
if ( GUI.Button ( Rect (0,0,130,30), "Missle) )
{
//activates the missleSpawn script
if ( missleSpawn )
{
missleSpawn.enabled = true;
}
// or if button is not pressed
else
{
missleSpawn.enabled = false;
}
}
}
If it does not work let met me know and i will fix it. Now here is the missleSpawn script. Make sure its component check box is not checked.
//the missle var missle = Rigidbody;
function Update () { // i know this will be placed on the MissleSpawn object but it still works if not let me know and i will see what i can do. var missle = Instantiate(missle, transform.Find("MissleSpawn").transform.position, Quaternion.identity);
//adds the force
Rigidbody.AddForce(forward * 600);
}
There now you have it. If you like this answer and you think its the best one you got rate it up if not rate it down then. I will be checking this every now and then see if you like this answer. Good bye
Your answer
![](https://koobas.hobune.stream/wayback/20220613064505im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Missile Collision and Explosions 1 Answer
how to make dust after explosion on objects 1 Answer
missile doesn't move 2 Answers
How to make an object rotate on the y axis towards the mouse? 3 Answers
Turret system not working c# 2 Answers