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
0
Question by Tea_Doogun · Jul 09, 2012 at 10:52 PM · mouselocation

Transform coordinates where the cursor clicks...

Using the mouse in Unity is something that seems to brings me trouble.

Could anyone tell me how to 'get' the xyz coordinates when the mouse clicks a location in the unity-space? Because i want to use these coordinates in a variable

Basicly: Var targetLocation = [Mouse-click coordinates]

When mouse button down x, y, z = location

I will then use the 'targetLocation' variable as i want.

Help?

Hope i made sense, Tom.

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

Answer by stingman · Jul 09, 2012 at 11:56 PM

Use Raycasting. Example from the docs:

 var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 if (Physics.Raycast (ray, 100)) {
     print ("Hit something");
 }

Read more on it here: http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html

All you need to do is return a Vector3 of where the mouse clicks

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 Tea_Doogun · Jul 10, 2012 at 10:43 AM 0
Share

Thanks alot for your help, i'll give that a go :)

avatar image
1

Answer by Muuskii · Jul 10, 2012 at 12:56 AM

I use something like this:

 private Plane targetingPlane;        //raycast to this plane to find where the mouse is pointing 
 
     Vector3 getCursor()       //Raycasts from current mouse position to the targetting plane, returns the resulting Vector3
      { 
      Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition ); // Construct a ray from the current mouse coordinates
      
      float hitdistance; 
      
      if (targetingPlane.Raycast (ray, out hitdistance )) return ray.GetPoint(hitdistance);
      
      return transform.position + transform.forward;  //just in case ;)
      }
     
     void Start () 
     {
      targetingPlane.SetNormalAndPosition(transform.up, transform.position);
     }


This is in c# because that's primarily what I use.

Hope that helps! :)

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 Tea_Doogun · Jul 10, 2012 at 10:45 AM 0
Share

I use JS, but i reckon i can figure out somthiung similar from looking at how your script works, thanks :)

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Update position of an object to match a high velocity object and still allow OnMouseOver() 1 Answer

Mouse click changes spawn location how do you do it? 4 Answers

Checking the position of the mouse cursor in 3d space 2 Answers

Movement In A Mobile Game 2 Answers

How do I create a shape that rotates from one pivot point, to always point towards the mouse cursor? 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