- Home /
Mouse Click and Spawn object
Hello,
i got this script, but it don't work as i need..
var bednaPrefab : Transform;
function Update () {
var mousex = Input.mousePosition.x; var mousey = Input.mousePosition.y; var ray = camera.main.ScreenPointToRay (Vector3(mousex,mousey,0));
if ( Input.GetMouseButtonDown(0) ){ var crate = Instantiate(bednaPrefab, ray.origin, Quaternion.identity); }
}
it works, but it is spawning crate somewhere in space :D I have that script attached in Main Camera of 2D Platformer tutorial... So i need to make it like this:
You will play as normal. Then you click somewhere, and crate will spawns. It will get your mouse x & y (z = 0) and then spawns it. Crates are spawning, i can see, they are falling down, but i cant see the crates... Only in hierarchy. Help me! Any solutions?
Answer by skovacs1 · Dec 07, 2010 at 05:37 PM
"it don't work" aside from being an abomination of the English language is also not very explicative. It tells neither how it failed to work nor in what context.
Instantiate does not take a ray as a position. You should try:
Instantiate(bednaPrefab, ray.origin, Quaternion.identity);
or more than likely
Instantiate(bednaPrefab, hit.point, Quaternion.identity);
Answer by pocikanec · Dec 07, 2010 at 06:03 PM
Hello sorry, but i haven't much time before...
now:
I want to spawn a crate - where i click with my mouse. I want only X a Y axis, Z will be 0 (side platformer game).
I want to click & then spawns a crate (in air) and then it falls.
So i attached that script to main camera & it gives me this error:
Assets/Scripts/qe.js(12,40): BCE0023: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(UnityEngine.Transform, UnityEngine.Ray, UnityEngine.Quaternion)' was found.
This error matches the answer written by skovacs1. You are feeding the Instantiate function with a Ray where it expect a Vector 3. Try one of the solutions given by skovacs1.
Answer by pocikanec · Dec 07, 2010 at 06:31 PM
I have rescripted it:
var bednaPrefab : Transform;
function Update () {
var mousex = Input.mousePosition.x; var mousey = Input.mousePosition.y; var ray = camera.main.ScreenPointToRay (Vector3(mousex,mousey,0));
if ( Input.GetMouseButtonDown(0) ){ var crate = Instantiate(bednaPrefab, ray.origin, Quaternion.identity); }
}
Now it works, but it is spawning crate somewhere in space :D I have that script attached in Main Camera of 2D Platformer tutorial... So i need to make it like this:
You will play as normal. Then you click somewhere, and crate will spawns. It will get your mouse x & y (z = 0) and then spawns it. Crates are spawning, i can see, they are falling down, but i cant see the crates... Only in hierarchy. Help me! Any solutions?
You're posting your follow-up questions as answers to your original question. Please add additional information and any follow-up questions you have either as comments or as edits to your original post ins$$anonymous$$d.
Answer by Earth-O-Matic · Mar 31, 2011 at 05:26 AM
This is a very old question I know but maybe some one will find this helpful. This works for me...
Should work - attach to main camera
var bednaPrefab : Transform;
function Update () {
var mousex = Input.mousePosition.x; var mousey = Input.mousePosition.y; var ray = camera.main.ScreenPointToRay (Vector3(mousex,mousey,0));
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 200)) {
} if ( Input.GetMouseButtonDown(0) ){ var create = Instantiate(bednaPrefab, hit.point, Quaternion.identity);
}
}
var mousex = Input.mousePosition.x; var mousey = Input.mousePosition.y; var ray = camera (Vector3(mousex,mousey,0));
has an error in it? for me is says: " is not possible to invoke an expression of type 'UnityEngine.Component'.
pls help
Your answer
Follow this Question
Related Questions
Object Movement via Mouse Click? 3 Answers
Spawn Objects Where i click 2 Answers
How to make object clickable when user enter collider ? 0 Answers
How to attach objects using a mouse click on one side, and object only attaches to that one side? 0 Answers
Getting raycasting info from mouse event (eg through RaycastHit) 0 Answers