Input.mousePosition vector3 in a Camera.ScreenToWorldPoint method not working properly when camera hits its boundary, unity2D
transform.parent.transform.position = thePlayer.position;
mouse = Input.mousePosition;
mouse = new Vector3(mouse.x, mouse.y, 1);
mouse = Camera.main.ScreenToWorldPoint(mouse);
Vector3 dir = mouse - thePlayer.position;
float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
print("" + mouse + "___" + thePlayer.position + "___" + dir);
transform.parent.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
now this works well and good when my camera has not stopped at its bounds, but when the camera does stop and the player moves to the bounds and i don't move my mouse cursor, the mouse vector keeps going up even when i am not moving the mouse, this then causes the 'mouse - thePlayer.position' Vector3 to be wrong and generate the wrong angle.
Iv'e been trying to figure it out and just can't come to a decent solution! The only things i could think of are the mouse pos to world pos is thinking the camera should have moved so much over the boundary and moves the still mouse that way in terms of world position even though it's stationary in terms of screen and world space.
In terms of boundaries, the camera simply cannot go past x / y -50 -> x / y +50, so i'd expect the maximum/minimum mouse position in world position terms would be 50 or -50 but it goes up to about +- 63, these extra values are when the player is moving when the camera has stopped at the boundary and mouse pos keeps increasing. The image below shows the positions of the mouse, player and the dir Vector3 as stated in the print() in my code. this is when the mouse is on the left side of the player, but the distances are far greater than they should be.
So yeah, thanks!
[1]: /storage/temp/63456-positions.png
when the camera does stop and the player moves to the bounds and i don't move my mouse cursor
I'm not sure that i understand. If 'mouse' is the cursor world position and thePlayer.position;
is the player world position, if either of them change in relation to the other, then 'Vector3 dir' and 'float angle' will change too.
According to your explanation 'mouse' should be constant but the player moves so sounds like the changing of the angle is expected.
What is the expected result and what did I misunderstand? :)
One thing i probably should have mentioned was the player is in the middle of the camera and the camera follows the player, but when the camera hits its boundaries, the player can move freely to that boundary without the camera following.
Basically i am setting boundaries for the camera and when the camera reaches that point, the camera won't move in that direction anymore, When the camera hasn't hit these boundaries the Vector 3 that the Input.$$anonymous$$ousePosition method gives would be the same as my players position if i put my mouse directly on top of the player, the problem is when the camera has say hit the left boundary and the player continually moves left until he hits the edge, if i were to then put the mouse directly on top of the player, the 2 positions in world space do not equal the same thing.
The angle is deter$$anonymous$$ed by taking the player position away from the mouse position, that generates a vector 3 like (0,1,0) which would then generate the right angle to make my object rotate to point upwards where the mouse is, but when the camera has hit the boundary and the player ran to the edge of the map, the mouse would have to be around the middle of the camera to be in the same place as the player, even though the player is not in the middle but is on the far left of the camera. :)
Your answer
Follow this Question
Related Questions
camera zoom/moveing and object position 0 Answers
Issue with getting the mouse position in 3D and moving a gameobject on the y & z axis 0 Answers
How to rotate a sprite after using LookAt in 3D 0 Answers
How do I draw a raycast with my mouse position that is relative to the main camera? 0 Answers
Is there an easier way to do this? 1 Answer