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
1
Question by Daryn · Aug 24, 2012 at 12:58 AM · c#positionmousefollow

Following mouse

Alright so I hate to ask a question on something so broad but im not sure were to start. I want a platform to follow my mouse when it moves. To do this i figured if i could find a way to convert my mouse position to a world position and set that to a variable then i could measure the change and work form there. The only thing is that i dont know how to do really anything with the mouse and so was wondering. How can I set the mouses position as a variable? also i use C#

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

Answer by Panik.Studios · Aug 24, 2012 at 01:26 AM

Did you try looking Here?

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 Daryn · Aug 24, 2012 at 04:21 AM 0
Share

Ya i couldnt really understand it thats why i came here

avatar image
0

Answer by aldonaletto · Aug 24, 2012 at 02:16 AM

You could cast a ray from the camera that passes through the mouse point, and get the position where this ray hits something - this is useful when you want the platform to keep a constant height above the ground:

float height = 3; // height above the ground Transform platform; // drag the platform here

void Update(){ // create a ray passing through the mouse pointer: Ray ray = camera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)){ // if something hit... // move the platform to the hit point: platform.position = hit.point + height * Vector3.up; } }

Comment
Add comment · Show 4 · 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 Daryn · Aug 24, 2012 at 04:23 AM 0
Share

God is all you do is sit on your computer and answer questions i see you everywere(i do not mean this as a bad thing). And ill give this a try.

Alright so i tried it and weather it worked or not im not tottaly sure as i left some out.(the part about the height as thats not what i need) So let me try to be a bit more specific all i need as i believe i can get the rest to work out is to be able to convert my mouse position into a vector2 world position.

avatar image aldonaletto · Aug 24, 2012 at 01:17 PM 0
Share

$$anonymous$$y wife says the same - but she thinks this IS a bad thing! (girls hate when we have fun without them...) The problem is: a 2D screen point corresponds to an infinite line in the 3D world. To get a single point, you must find where this line intersects a plane (or the ground, in the case above). If you place a big plane in front of the camera (at some distance - 20 units, for instance), the script below can return a reliable 3D point:

Vector3 hitPoint;

void Update(){ // create a ray passing through the mouse pointer: Ray ray = camera.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)){ // if something hit... // get the hit point: hitPoint = hit.point; } } The hitPoint variable has the 3D coordinates of the plane point that lies "under" the mouse pointer.
NOTE: Attach this script to the camera

avatar image Panik.Studios · Aug 25, 2012 at 08:13 PM 1
Share

"$$anonymous$$y wife says the same" Hey I appreciate anyone taking the time to review and answer my questions. So thanks for supporting the community.

avatar image Daryn · Aug 25, 2012 at 09:28 PM 0
Share

ill be trying that soon i just have a few more codes to right before im at that point again

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

9 People are following this question.

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

Related Questions

Camera rotation around player while following. 6 Answers

Move an object towards a point using raycast from Main Camera 2 Answers

Multiple Cars not working 1 Answer

Camera Rotate to mouse position -2 Answers

An Efficient Way To Follow Transform's Position? 3 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