Object follow Cursor - How to access mouse cursor x and y position
I have looked this question up and found plenty of answers, only every answer is code with errors, or it just plain didn't work. Here is what I have written myself which works (C#):
void Update () {
this.transform.position = Input.mousePosition;
}
BUT this positions the object out of my camera view.
If I try to access the y or x coordinate alone I get errors.
"Cannot modify a value type return value of 'UnityEngine.Transform.position'. Consider storing the value in a temporary variable"
If I try storing these values in a variable I get
"Unexpected symbol `=' in class, struct, or interface member declaration"
What is the proper way of doing this? Thanks
Answer by tanoshimi · Feb 17, 2015 at 05:19 PM
I doubt that this.transform.position = Input.mousePosition;
is really what you want. transform.position is measured in world units in 3d space. mousePosition is in pixel units in 2d space, so mapping one directly to the other makes little sense. Instead, you need to use the ScreenToWorldPoint method.
Something like this (untested):
void Update () {
Vector3 temp = Input.mousePosition;
temp.z = 10f; // Set this to be the distance you want the object to be placed in front of the camera.
this.transform.position = Camera.main.ScreenToWorldPoint(temp);
}
edit: okay after playing around with this code, I can see that it is following the cursor x and y BUT the z coordinate doing something funky, it moves around sparatically until it settles in at 0,0,0. Really strange
You need to put this on a script attached to the gameobject you want to move. And then press the Play button.
Perhaps you could explain what you were looking for and how it differed from what this gave you? Try hardcoding the z value to something else ins$$anonymous$$d (my original code sample assumed the camera and object were parallel - I've modified in case this isn't correct)
Hey @tanoshimi this worked beautifully, thanks. However, is it possible for you to explain it further? I like when things work just fine, but I would also like to know how and why they work.
Input.mousePosition gives you the 2d coordinates of the mouse cursor relative to the corner of the screen.
transform.position is the 3d coordinates of an object relative to the origin of the world.
So this code first provides an arbitrary "depth" z coordinate to the mouse cursor and then converts the coordinates to world space using ScreenToWorldPoint
"You need to put this on a script attached to the gameobject you want to move. And then press the Play button." - lol
This modified code works well, thanks tanoshimi!
Answer by casteponters · Feb 17, 2015 at 05:23 PM
Try this for mousePosition:
Vector2 mousePosition = new Vector2 (Camera.main.ScreenToWorldPoint (Input.mousePosition).x, Camera.main.ScreenToWorldPoint (Input.mousePosition).y);
Your answer
Follow this Question
Related Questions
How to make an object look in the direction of the mouse cursor? 0 Answers
2D - Object follow cursor on X-axis. 0 Answers
Terrain to object? 0 Answers
My clone objects behind background 0 Answers