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 Kyle_Katarn · Apr 04, 2014 at 06:58 AM · mousescreentoworldpointmouse-drag

Update ScreenToWorldPosition for Moving Camera

Hello all.

I'm trying to draw an arrow (it'll eventually be an arrow, right now, it's just a Debug.DrawLine) on the screen with a mouse using ScreenToWorldPosition. That system is fine and currently works, but the problem is, my player is constantly moving, so the initial mouse click sticks in the one place, and slowly crawls off the screen. What I want to do is have that initial click move along with the player, or camera itself, but when I try to add the position to the initial mouse click position, it winds up being offset way off the screen by a large amount.

My code is currently as follows:

 // Get if the mouse is clicked and held
 if (Input.GetMouseButtonDown(0))
 {
     _mouseClickPosition = Camera.main.ScreenToWorldPoint( new Vector3(Input.mousePosition.x, Input.mousePosition.y, 20));
  
     _isDragging = true;
 }
  
 // if you want to input an aiming arrow, do it here
 if (_isDragging)
 {
     Debug.DrawLine(
         _mouseClickPosition,
         Camera.main.ScreenToWorldPoint( new Vector3( Input.mousePosition.x, Input.mousePosition.y, 20 ) ),
         Color.white );
  
 }
  
  
 // mouse released
 if (Input.GetMouseButtonUp(0) && _isDragging)
 {
     _mouseReleasePosition = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 20));
  
     _isDragging = false;
 }

That achieves the original problem, but if I add the players position (or the camera's position) to the _mouseClickPosition variable, it offsets the original click to a large degree. What I want is for that original mouse click to stay where it is on the screen while the player moves.

The camera setup is above the player, looking down at a slight angle, while the player is moving along the XZ axis.

Thanks for reading, and I hope you can help me, this has me stumped.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by robertbu · Apr 04, 2014 at 07:08 AM

The easiest solution is to save the original mouse position and do the ScreenToWorldPoint() each time the value is used. So _mouseClickPosition and _mouseReleasePosition become screen cooridnates, and you call ScreenToWorldPoint() each time you call Debug.DrawLine() ( or draw the arrow as you will eventually do).

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 Kyle_Katarn · Apr 04, 2014 at 07:31 AM 0
Share

Brilliant, thank you so much! :D

I shall now proceed to kick myself for not thinking of something so simple!

Thanks 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

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

Related Questions

mouse Input Instantiate 0 Answers

Why is OnDrag triggering but not OnMouseDown? 0 Answers

Can I change GUIButton behaviour? 1 Answer

Mouse drag gameobject 0 Answers

Tracking slow mouse movements 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