- Home /
Spawn prefab where i click.
Ok so i have a basic script whitch spawns an item when i click my mouse, however i would like it so the object spawns where i click, instead of from the object i attach it to. This is the script:
var bomb : GameObject;
function FixedUpdate(){
//0 is for when the left button is clicked, 1 is for the right
if(Input.GetMouseButtonDown(0))
UnityEngine.Object.Instantiate(bomb,transform.position,Quaternion.identity);
}
In case you are wondering i am making a ragdoll type game and would like it so that where i click a grenade appears and explodes (i have already got the exploding and stuff sorted so i dont need any help on that).
Answer by whydoidoit · May 29, 2012 at 05:13 PM
Ok so you need to convert the mouse position to a world position - which means you need to figure out how far deep into the screen you want the item to appear.
Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, Camera.main.nearClipPlane)
Would get you the Vector3 of the position at the nearest visible point on the camera - you might want something deeper of course.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Mouse Click and Spawn object 4 Answers
Spawning objects help 2 Answers
Randomize Spawn Time 2 Answers
What is the reason for this NullReferenceException? 1 Answer