• 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
1
Question by VentWs · Sep 06, 2013 at 02:24 AM · transformpositiontransform.positionmouse position

Getting Transform position using mouse position

I am trying to display the Transform position of a GameObject. When the mouse click on any part of the GameObject, it will display the current Transform position that the mouse position is currently at. If it is possible please show me how the code works.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by getyour411 · Sep 06, 2013 at 02:40 AM

Look at the doc for further information about Input.mousePosition and Raycast (C#). You want something like this:

     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     Physics.Raycast (ray, out hit);

hit.transform is your GameObject transform

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

Answer by Ozcorps · Sep 06, 2013 at 02:43 AM

This is what your looking for, this is the best way ive found from searching and seeing it in tutorials.. hope this helps

 Camera.ScreenPointToRay
 ScreenPointToRay(position: Vector3): Ray;
 Description
 
 Returns a ray going from camera through a screen point.
 Resulting ray is in world space, starting on the near plane of the camera and going through position's (x,y) pixel coordinates on the screen (position.z is ignored).
 
 Screenspace is defined in pixels. The bottom-left of the screen is (0,0); the right-top is (pixelWidth,pixelHeight).
 
     // Draws a line in the scene view going through a point 200 pixels
     // from the lower-left corner of the screen
     function Update () {
         var ray : Ray = camera.ScreenPointToRay (Vector3(200,200,0));
         Debug.DrawRay (ray.origin, ray.direction * 10, Color.yellow);
     }
 
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
avatar image
0

Answer by Hyperion · Sep 06, 2013 at 02:44 AM

For the clicking part, you would want to use

 function OnMouseDown () {
 
 }

What do you mean by 'display'?

If you want to find the position of the mouse, you'd have to attach the following to the camera:

 static var mousepos : Vector3; //this is going to be the position of the mouse
 
 function Update ()  //happens every frame 
 {
     var ray = Camera.main.ScreenPointToRay (Input.mousePosition);  //making ray point to mouse (invisible)
     var hit : RaycastHit;
     if (Physics.Raycast (ray, hit,1000))
     {
         mousepos = hit.point;
     }
 }

I'll assume by 'display', you mean print. So you'd have to do

 function OnMouseDown(0){  //0 means left click
 print(mousepos);
 }

If this answer helped, please accept it.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

17 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

Related Questions

How do i make my background keep the same position as my character? 0 Answers

Removing Vector3 Decimal for Position 2 Answers

My character's y position gets reset on play. 0 Answers

Transform.position not working ??? 0 Answers

Setting transform.position not behaving properly 2 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