So I have been trying to solve this issue for a long time now. Basically, I am trying to simulate the mouse in order to set the mouse cursor on a specific object after clicking on a button. After some research, I have found this method: private static extern bool SetCursorPos(int X, int Y).
However, when I try to pass the collider’s position in that method, as the values need to be converted first, but I have no idea how to do so.
I would really appreciate it if someone could help me out. The project is for my thesis and I running out of time.
Hey @ssoliman1886 . Yes you can force the mouse to move with this method all you have to do is give the method desire parameter in screen coordinate. your collider position belongs to word coordinate system so if you want to move your mouse cursor to new position on screen you should convert your world position coordinate to screen coordinate .
For the purpose of converting you can use this method :
Vector3 colliderScreenPos = Camera.main.WorldToScreenPoint(collider.transform.position);
this colliderScreenPos have 3 component :
- (X) = Desire position in pixel in x axis
- (Y) = Desire position in pixel in y axis
- (Z) = The z position is in world units from the camera
Screenspace is defined in pixels. The bottom-left of the screen is (0,0); the right-top is (pixelWidth,pixelHeight).
now try to use your method :
SetCursorPos((int)colliderScreenPos.x,(int)colliderScreenPos.y):
Final node : I Think this method will work just in build and full screen . this means it will not work in editor . try it and let me know the result
Best regards
Fariborz