Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by ItsKhanny · Oct 04, 2015 at 08:03 PM · c#

how to make a game object follow the mouse cursor

How do I get it to where I make a game object stays under the cursor. No other answers are clear so please help.

edit: I also want to know if it could be in a grid like area so instead of being free move, it snaps

Comment
Add comment · Show 3
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 meat5000 ♦ · Oct 04, 2015 at 09:02 PM 0
Share

https://www.google.co.uk/search?q=unity+drag+an+object&ie=utf-8&oe=utf-8&gws_rd=cr&ei=rZ$$anonymous$$RVu3XEcj1UNOpn9gD

https://www.google.co.uk/search?q=unity+snap+to+grid&ie=utf-8&oe=utf-8&gws_rd=cr&ei=vZ$$anonymous$$RVp-2P$$anonymous$$u4UcnYsfAD#q=unity+drag+object+snap+to+grid

Please make an attempt and post a new question if you find you are truely stuck.

avatar image ItsKhanny meat5000 ♦ · Oct 05, 2015 at 10:23 AM 0
Share

I've already looked, I usually don't just post questions before looking online for a solution.

avatar image meat5000 ♦ ItsKhanny · Oct 05, 2015 at 03:56 PM 0
Share

Glad to hear it.

However there is a certain lack of attempt in this question.

Ill give you the benefit of the doubt. Post something that you have tried, or this question will need to be closed.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Wiki

Answer by Firedan1176 · Oct 04, 2015 at 09:23 PM

There's many approaches to how you want to do this, and it all depends on what you want to achieve. You can make your object follow your mouse without depending on a Z coordinate, and you could also have your object be aligned to another object (such as a ground), if the mouse is over that object.

  • First method

You can use Camera.main.ScreenToWorldPoint(Input.MousePosition) to get your mouse position in world space as a Vector3. Instead of using that exactly, try using this:

 float zAxis = 2f;
 Vector3 mousePosition;
 
 void Update() {
     mousePosition = Camera.main.ScreenToWorldPoint(Input.MousePosition);
     mousePosition.z = zAxis;
     //If you get an error with the above line, replace it with this:
     //mousePosition = new Vector3(mousePosition.x, mousePosition.y, zAxis);
 
 }

When you use ScreenToWorldPoint to get mouse position, the Z axis will either be really strange, or 0.

You can then use the Vector3 mousePosition to set the transform.position of another object to where the mouse cursor is in world space.

  • Second Method

If you have a floor in your scene, you could also raycast from your mouse cursor to plant your object on the ground.

Try using this code to achieve this:

 RaycastHit hit;
 Ray ray;
 
 void Update() {
 
     ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     if(Physics.Raycast(ray, hit, Mathf.Infinity) {
         transform.position = hit.point;
     }
 
 
 }

You can place this code on an object to make it align to the mouse when the mouse is over any object. This is untested, so I'm unsure of any errors. Also keep in mind that any object will be placed on your ground at the center/pivot point; For example, Unity's primitive cube will be halfway into the ground.

Comment
Add comment · Show 5 · 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 ItsKhanny · Oct 04, 2015 at 09:45 PM 0
Share

Im going with method 2 since Im using a floor, but how can I do it so when I click a GUI button, an object then follows the cursor

avatar image Firedan1176 ItsKhanny · Oct 05, 2015 at 01:12 AM 0
Share

You will need to use the OnClick() field of Unity's UI buttons to call your own function, and only do the updating from my above script if a bool is set true. You can set this bool to true once you call a function in the OnClick field as mentioned. Look at this.

If my answer above answered your question, please accept it as an answer.

avatar image ItsKhanny · Oct 05, 2015 at 10:20 AM 0
Share

I've managed to fix the problems with the script, and now I am getting an error saying "NullReferenceException: Object reference not set to an instance of an object Follows$$anonymous$$ouse.Update () (at Assets/Scripts/Follows$$anonymous$$ouse.cs:17)" I cannot add a picture of the error because of unity answers not letting me

avatar image meat5000 ♦ ItsKhanny · Oct 05, 2015 at 04:02 PM 0
Share

Post it on http://postimage.org/ and link it. Its all we can do until functionality is fixed.

avatar image ntgCleaner · Mar 08 at 06:41 PM 0
Share

FYI, the if line (7) should read like so:

if(Physics.Raycast(ray, out hit, Mathf.Infinity)) {

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.

Follow this Question

Answers Answers and Comments

31 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OnTriggerStay2D when trigger is spawned on object not moving 1 Answer

Destroy specific object with tag 2 Answers

Instantiate Previous Prefab in Array 0 Answers

I guys I have a really hard question 0 Answers

if a value of a variable is set on the server using [Command] for the local player is the value set on the local player locally? 0 Answers


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