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 ticofa · May 14, 2015 at 04:26 AM · mousepositionvector2offsetorthographicscreentoworldpoint

Problem with getting Mouse Position 2D View

Hi there :)! I have a slight problem with getting correct the mouse position in ScreenToWorlPoint...

The thing is... if i do not move my player, i get the correct value of the position of the mouse... and thus, the player gets its size inverted, to give the apearence of facing the other side...

If its on the right side of the screen, it has a positive value... and if its on the left side, negative value...

The problem rises when i move the player, its like the mouse its self gets offset as well...

So if i move my player to the right... and point the mouse to the left of the screen, it still gives me a positive value... And the inverse happens if i move my player to the left...

Please help me :S!

Heres the code i use to get the mouse position:

 public float _x;
 
 // Use this for initialization
 void Start () {
     
 }
 
 void RotatePlayer()
 {
             Vector3 _screeMouse;
             Vector3 _worldMouse;
     
     
             _worldMouse = Camera.main.ScreenToWorldPoint (Input.mousePosition);
             _x = _worldMouse.x;
             if (_x < 0) {
                     this.transform.localScale = new Vector3 (1, 1, 1);
             } else if (_x > 0) {
                     this.transform.localScale = new Vector3 (-1, 1, 1);
             }

     }
 
 // Update is called once per frame
 void FixedUpdate () {
     RotatePlayer ();
 }
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

Answer by sas_88 · May 14, 2015 at 05:19 AM

It works fine for orthographic camera.If u move the mouse in & out the screen the scale remains same.

 void Update  () 
 {
   Vector3 pos=Camera.main.ScreenToWorldPoint (Input.mousePosition);
   pos.z = 0;
   transform.position = pos;
 }

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 ticofa · May 14, 2015 at 06:35 PM 0
Share

Oh yeah i know that, it's my fault for lack/bad choice of information on the question...

What i want to do is to get the value for the X position of the mouse relative to the screen... if its bigger than 0, then my player object faces right... if its lower than 0 then player faces left...

The problem is, when i move the player, the position becomes offset...

so, if i start the game and not move at all... the X value of the mouse position is correct and the player turns... but if i move to any direction... the X value of the mouse position, gets offset by the same amount of distance i moved :S

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

when mouse click a gameobject ,how can I get the position of mouse click gameobject 2 Answers

Rotate sprite to face direction of movement 1 Answer

Convert Mouse Position to move a cube on 1-axis. 1 Answer

Input.GetAxis with MouseMovement 0 Answers

How can I find out a ViewportToWorldPoint position with a different orthographic size? 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