x


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?

more ▼

asked Dec 07 '10 at 05:20 PM

pocikanec gravatar image

pocikanec
21 4 4 7

(comments are locked)
10|3000 characters needed characters left

4 answers: sort voted first

"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);
more ▼

answered Dec 07 '10 at 05:37 PM

skovacs1 gravatar image

skovacs1
10k 11 25 92

(comments are locked)
10|3000 characters needed characters left

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.
more ▼

answered Dec 07 '10 at 06:03 PM

pocikanec gravatar image

pocikanec
21 4 4 7

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.

Dec 07 '10 at 06:13 PM bjarnefisker
(comments are locked)
10|3000 characters needed characters left

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?

more ▼

answered Dec 07 '10 at 06:31 PM

pocikanec gravatar image

pocikanec
21 4 4 7

please, help me!

Dec 07 '10 at 08:32 PM pocikanec

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 instead.

Dec 07 '10 at 09:40 PM Jesse Anders

edited... now it shows in first post

Dec 07 '10 at 10:22 PM pocikanec
(comments are locked)
10|3000 characters needed characters left

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);

}

}

more ▼

answered Mar 31 '11 at 05:26 AM

Earth-O-Matic gravatar image

Earth-O-Matic
70 9 9 18

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1579
x1120
x1001
x452
x232

asked: Dec 07 '10 at 05:20 PM

Seen: 4512 times

Last Updated: Dec 07 '10 at 10:22 PM