Before updating to Windows 10 you may need to return your Pro license. More information here

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

avatar image

14 12 15 19

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, 2014 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

avatar image

931 57 67 81

(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



Answers and Comments



asked: Aug 09, 2013 at 08:19 PM

Seen: 895 times

Last Updated: Oct 22, 2014 at 05:53 AM