Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MikezNesh · Jul 12, 2010 at 07:35 PM · explosionturretmissile

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MikezNesh · Jul 12, 2010 at 08:09 PM 0
Share

How would I do that?

avatar image Tetrad · Jul 12, 2010 at 08:24 PM 0
Share

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.

avatar image MikezNesh · Jul 12, 2010 at 08:51 PM 0
Share

Added script for my shooting...dunno how to explode when reaches where I clicked.

avatar image Tetrad · Jul 12, 2010 at 09:14 PM 0
Share

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.

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges