Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
3
Question by Showken · Oct 22, 2010 at 04:01 AM · mousepositiondrag-and-dropscreentoworldpoint

when mouse click a gameobject ,how can I get the position of mouse click gameobject

when mouse click a gameobject ,how can I get the position of mouse click gameobject. I want drag a gameobject and move

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
5

Answer by zannghast · Oct 22, 2010 at 07:04 AM

The answer would depend on whether you wanted to get the position of the mouse click in screen coordinates or game world/space coordinates.

If it was the former, getting the position in screen coordinates usually means using something like:

Vector3 clickedPosition = Input.mousePosition;

If it was the latter, then you'd need to use something like the code below to get the position of the mouse click in game world/space coordinates:

Vector3 clickedPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

To know more about other Input functions, head on over to the official Scripting reference page here: Link

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 Showken · Oct 22, 2010 at 09:40 AM 0
Share

thk you!it's latter!I want to get the coordinates when ray hit

avatar image zannghast · Oct 22, 2010 at 10:00 AM 0
Share

Glad you got what you were looking for. Be sure to have your camera oriented correctly, though. $$anonymous$$oving objects in a 3D space based on the 2D coordinates of the mouse usually implies you've already decided on how the objects will move in the game and designed any specific translation of the 2D mouse coordinates. If you need elaboration on what I just said, just tell me and I'd be glad to help you out, although I might not be able to answer immediately in the next couple of days.

avatar image zannghast · Oct 22, 2010 at 10:10 AM 0
Share

By the way, now that you've mentioned using a ray, you could use Camera.main.ScreenPointToRay(Input.mousePosition) ins$$anonymous$$d.

avatar image Showken · Oct 23, 2010 at 06:55 AM 0
Share

thank you ,I am a beginner from China. I want to drag a gameobject with mouse. but the position of object must be same with mouseposition. I try to use ScreenPointToRay(Input.mousePosition),but the value of return is ray. I have not knew how to get effect what I want to . can you give me some tips? thank you very much!

avatar image zannghast · Oct 26, 2010 at 01:28 AM 0
Share

Hi Showken, this would largely depend on how you intend the player to view your game. For instance, do you use a purely 2D side-scrolling camera like in LittleBigPlanet for instance. Or maybe a purely top-down view like in the EVAC-City Tutorial. Or do you use a semi top-down approach (sort of like isometric) like in Sonic 3D for the $$anonymous$$ega Drive.

The answer to your question with regards to 'moving' your gameobject would depend on how complex your camera setup is. If you could post more details about that, it would help people assist you better hehe.

avatar image
0

Answer by GFX47 · Apr 04, 2011 at 02:31 PM

Here's a complete solution for right click detection: http://blog.gfx47.com/2011/04/04/detect-right-click-on-game-objects-in-unity3d/ Hope that will help! ;)

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

ScreenToWorldPoint error 3 Answers

Wrong world point mouse position 1 Answer

Rotate sprite to face direction of movement 1 Answer

Drag-and-drop gameObject inversely proportional to mouse speed? 0 Answers

screenToWorld not working 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