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 ekanitegames · Jun 22, 2015 at 06:42 PM · spritemousepositionaspect-ratioscreensize

Input.mouseposition affected by game scene size

I am trying to position a sprite based off of mouse position. My code:

 void Update () 
 {
 
     X = Input.mousePosition.x;
     Y = Input.mousePosition.y;
     follow.transform.position = new Vector2 (X, Y); 
 }

"follow" is a public Game Object defined earlier in the code. The code functions almost as expected, except that if I re-size the Game Scene, follow's center is no longer positioned at the cursor's position.

At default 16:9 aspect game scene view: alt text

At 16:9 aspect game scene view that is resized: alt text

I'm relatively new to unity so I'm no expert. I'm guessing that this issue is due to the follow sprite being automatically re-sized based off of the game scene while the mouse-position reference is unaffected by the screen size. Any help would be greatly appreciated, thanks.

question2-slide-1.png (37.4 kB)
question-2-slide-2.png (22.4 kB)
Comment
Add comment · Show 1
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 maccabbe · Jun 22, 2015 at 07:39 PM 2
Share

http://docs.unity3d.com/ScriptReference/Camera.ScreenToWorldPoint.html

2 Replies

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

Answer by Eno-Khaon · Jun 22, 2015 at 07:51 PM

Input.mousePosition returns "The current mouse position in pixel coordinates. (Read Only)". As @maccabbe stated, what you'll want to use is Camera.ScreenToWorldPoint in order to transform that position into a location in your game world.

 void Update()
 {
     Vector3 worldMousePos = Camera.Main.ScreenToWorldPoint(Input.MousePosition);
     follow.transform.position = new Vector2(worldMousePos.x, worldMousePos.y);
 }
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 chrismkhill · Jun 22, 2015 at 08:53 PM

Hey there,

Input.mousePosition is "The current mouse position in pixel coordinates"

Try instead this which will convert it to world coordinates.

 X = Camera.main.ScreenToWorldPoint(Input.mousePosition).x;
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

(solved) 2d sprite rotation towards mouse 1 Answer

Vertex shader coordinates in clip space 1 Answer

how to have a sprite follow the spot of my mouse cursor. 0 Answers

How do I anchor sprite positions to an Image with "Preserve Aspect" set? 1 Answer

Sprite in different aspect ratio 0 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