- Home /
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 ();
}
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;
}
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
![](https://koobas.hobune.stream/wayback/20220613195547im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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