- Home /
Explosion at mouse
What would the script be to instantiate an Explosion prefab exactly where you click on the screen? Such as in the Detonator Package, where you click, you make an explosion.
any help on this?
Answer by 3dDude · Nov 28, 2010 at 01:51 AM
well it depends... do you want a explosion to appear on the point were you click on a object?
Or, just have them in mid air at x distance away...
you would have to use raycasting...
here is a script that I wrote that creates a explosion on the point were you clicked:
//ExplosionAtPoint.js
var explosionPrefab : Transform; function Update () { if(Input.GetKeyDown("mouse 0")){ var ray = camera.ScreenPointToRay(Input.mousePosition); var hit : RaycastHit; if(Physics.Raycast(ray,hit)){ var rot = Quaternion.FromToRotation(Vector3.up,hit.normal); Instantiate(explosionPrefab,hit.point,rot); } } }
note: all the objects you want explosions on have to have colliders....
hope this help...
hey thanks, but where do you put the ExplosionAtPrafab script?
ok sorry for not replying... unityAnswers never notified me of you replying and I forgot to check :(
anyway... you need to put this script on the main camera :)
glad to help! If you find the answer helpful click the checkmark so people now that it is answered!
Your answer
Follow this Question
Related Questions
Instantiate Object At Local Position 2 Answers
Cast Source Instantiating Error 1 Answer
Collider.bounds position is wrong? 3 Answers
Instantiate a prefab at the Mouse's position? 1 Answer
Instantiate PreFab with C# Script 5 Answers