• 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 /
avatar image
0
Question by zak666 · Jan 15, 2016 at 10:24 AM · scripting beginner

Make GameObject follow the mouse (non click just more mouse and go in that direction.)

Basicly for movment in my game i want to move the mouse and have the player move and rotate towards mouse position. C#. all have been click to move scripts i have searched on googleyeyes and have not been useful. (not click to move, just follow the direction the mouse is to the screen) fast paced top down game were clicking would hurt gameplay. looking for pointers or tutorials :D was thinking along the lines of "LookAt mouse position"

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by hexagonius · Jan 15, 2016 at 11:46 AM

If you're in top-down 3D LookAt will work. But you'd need to transfer your mouseposition, which is in the 2d coordinate system of your screen into the game world. To do that use
Camera.ScreenToWorldPoint(Input.mousePosition);

The command will return a position in the near clipping plane of the camera (the white rectangle close to the camera in scene view). if you first set the z value of the vector to change, e.g. to 10, the world space vector will be 10 units from the near clipping plane. This approach works for 0° views onto the game scene if you know the distance from camera. If you're looking at an angle however you would need to raycast into the scene to get a point on the ground (Use Camera.ScreenPointToRay and Physics.Raycast for that) In either case, set the "up component of that vector", relative to the player, to its center y position. Now you have a vector that is at the same level as the player and it can look at it and move towards it.

HINT: I know, that in the beginning one just wants something to work, but will get even more frustrated when it gets harder and one didn't even understand what the codes they copied from the internet did... Write one yourself.

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 zak666 · Jan 15, 2016 at 01:24 PM 0
Share

I try and wright everything myself so later on i understand what im doing rather then copy paste code and not learn nothin :D I'll look into htis and give it a go

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

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

Related Questions

Connection system like polybridge or turmoil 1 Answer

How to grab, store and reference an objects position on trigger? 1 Answer

Making a button appear on a specific state? 1 Answer

Can't seem to get dash cooldown working... 1 Answer

How to stop all StopAllCoroutines(); from another script 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