• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
2
Question by BadSeedProductions · Feb 17, 2015 at 05:14 PM · objectcursor

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
15
Best Answer

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



Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BadSeedProductions · Feb 17, 2015 at 05:39 PM 0
Share

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

avatar image tanoshimi · Feb 17, 2015 at 05:45 PM 0
Share

You need to put this on a script attached to the gameobject you want to move. And then press the Play button.

avatar image tanoshimi · Feb 17, 2015 at 05:53 PM 0
Share

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)

avatar image BojoXZ tanoshimi · Jan 03, 2017 at 12:37 AM 0
Share

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.

avatar image tanoshimi BojoXZ · Jan 03, 2017 at 07:19 AM 2
Share

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

Show more comments
avatar image BadSeedProductions · Feb 17, 2015 at 06:13 PM 0
Share

"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!

avatar image gitlinjoss · Apr 24, 2020 at 11:37 PM 0
Share

Awesome trick. Worked beautifully.

avatar image
0

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);
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BadSeedProductions · Feb 17, 2015 at 05:40 PM 0
Share

Lots of errors

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D - Object follow cursor on X-axis. 0 Answers

How to make an object look in the direction of the mouse cursor? 0 Answers

Make an object move from Point A to Point B then back to Point A repeating 9 Answers

Unity 3D C# - Counting Objects 0 Answers

Create a UI and place onto object 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges