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
4
Question by maikkel · Sep 24, 2010 at 07:57 PM · mouseposition

get mouse position on object

Hi, I'm a noob, so please be gentle...

I have a plane and want to convert the Screen Mouse Position to the position on that plane. (for some kind of strategy game) What's the best (most efficient) way to do this? Raycasting or using ScreenToWorldPoint? Either way I can't get it to work. Does anyone have some example code?

Thanks for your help!

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
6
Best Answer

Answer by skovacs1 · Sep 24, 2010 at 08:46 PM

You would likely want to raycast. You could use ScreenToWorldPoint and build your own ray or you could just use raycast with ScreenPointToRay to save yourself the effort.

How you choose to raycast is up to you. You can use a physics raycast which requires you have a collider on the object you are casting against and that it not be on the ignoreRaycast layer or you could raycast against the collider explicitly or you could raycast against the plane itself which is computationally somewhat cheaper, but only negligibly so.

//Using physics var hit : RaycastHit; if(Physics.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), hit)) //we hit

//Raycast against a specific collider (plane is a gameObject or Transform) if(plane.collider.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), hit)) //we hit

//hit.Point would be the point in the world where the ray hit.

//Raycast against the plane itself (plane is a Plane) var enter : float; if(plane.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition))) //we hit

//enter gives us the distance along the ray, so //mainCamera.ScreenPointToRay(Input.mousePosition).getPoint(enter); //would give us the point in the world where the ray hit.

You say you want the point on the plane. For most purposes, having the world position is enough. If you actually need to know a point relative to the plane, then, assuming that your plane's transform pivot is properly centered and aligned, you would convert to object space and get the point relative to the plane.

//This is the position relative to the center of the plane.
//We should only need to care about the x and y coordinates as z should be 0.
var localPoint : Vector3 = plane.transform.InverseTransformPoint(hit.point);

Do as you like with that. If you need coordinates in some other system relative to the plane's scale or something, the calculation is straightforward.

Comment
Add comment · Show 2 · 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 maikkel · Sep 24, 2010 at 08:54 PM 0
Share

Wow, thank you very much for the fast answer! I used the collider version and it works perfectly! I love the Unity community!

avatar image amarneethi · Feb 12, 2012 at 01:27 PM 0
Share

hi skovacs1, can you please put a c# example. thanks

avatar image
3
Wiki

Answer by amuzulo · Mar 27, 2012 at 06:47 PM

I personally used this C# code based on the code from Scott Kovacs:

 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 RaycastHit hit;
 // Casts the ray and get the first game object hit
 Physics.Raycast(ray, out hit);
 Debug.Log("This hit at " + hit.point );

I already knew that there was a hit, so my code doesn't check if there is no hit. If there is no hit, this code returns (0.0, 0.0, 0.0).

Comment
Add comment · Show 3 · 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 samsmithnz · Sep 10, 2013 at 12:10 AM 0
Share

This is a great answer - simple AND works!

avatar image Sharpless512 · Jul 28, 2015 at 04:13 PM 0
Share

If you want the name of the object hit just use:

Debug.Log("This hit at " + hit.transform.name );

avatar image isteffy · Aug 27, 2015 at 06:43 AM 0
Share

This code has an error and is possibly deprecated because it returns (0.0,0.0,0.0) every time.

Your code has been placed in a void Update and the script has been attached to the $$anonymous$$ain Camera.

avatar image
0

Answer by Neil Smith · Mar 21, 2014 at 10:21 AM

This give the true world position. Just create a Javascript file with this code in it and add it to the your main camera.

 function Update(){
     if(Input.GetMouseButtonDown(0)){
         var hit: RaycastHit;
         var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if(Physics.Raycast(ray, hit)){
             Debug.Log('Hit point: ' + hit.point);
         }
     }
 }
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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to make player's projectiles move to the mouse click position in a 3D top down shooter game? 0 Answers

Create GUI at mousePosition. 1 Answer

GUI Texture follow the mouse 1 Answer

How to get the mouse direction while left click is pressed? 1 Answer

Trigonometry: finding the vector3 positon of one corner using one side and one angle. 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