x


How to move a GameObject to the mouse position?

I have this so far:

pragma strict

var cardmove : GameObject;

function Start () { cardmove.gameObject.transform = KeyCode.Mouse0; }

function Update () {

}

I am trying to move the cardmove Gameobject that i assign, but when I try to add the Keycode.Mouse0 to my code I get a read only error? How can I fix the read only error and get my script to work? Can someone help me with this thanks!

more ▼

asked Aug 09, 2013 at 08:19 PM

thecookie12 gravatar image

thecookie12
9 9 14 16

This is pretty simple. You can use the Vector3.Lerp function to achieve this. Use raycasting to get the mouse click position or the touch position. Then use the initial and the final position in the lerp function. The initial position being the position that the gameobject is at now and the final position being the click / touch position. You can find the article by The Game Contriver on the same here

Move to Touch / Click Position - The Game Contriver

Oct 22 at 05:53 AM falconer
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

you cant set a enumeration value (KeyCode.Mouse0) effectively an int to a Transform(a much more complex struct then anyone gives it credit)

if you want the Vector2 mousePosition Reletive the screen you would use Input.mousePosition though you will need to do work of this to get anything of real value in 3d space.

if you want to know like where the mouse interacts with respect to an object then you would use screen point to ray with respect to the mousePosition

more ▼

answered Aug 09, 2013 at 08:45 PM

gardian06 gravatar image

gardian06
891 56 67 79

(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:

x3703
x1473
x159
x99

asked: Aug 09, 2013 at 08:19 PM

Seen: 702 times

Last Updated: Oct 22 at 05:53 AM