- Home /
rotating to mouse location
i tried to rotate the arm in my main character to the mouse location but instead my code manges to rotate it in some weird direction 'when the character' moves only.. note: im not sure if this will make a difference but i have a static main camera (not moving)
Answer by robertbu · Nov 07, 2014 at 06:40 AM
This may not be your issue, but the code you have here will only work for an Orthographic camera. This:
Camera.main.ScreenToWorldPoint(Input.mousePosition)
...will always return the position of the camera for a perspective camera. For a perspective camera, you will need to set the 'z' parameter to a distance in front of the camera. So assuming the distance you wanted was 10, you would do:
Vector3 pos = Input.mousePosition;
pos.z = 10.0f;
Vector3 difference = Camera.main.ScreenToWorldPoint(pos) - transform.position.
thanks :D now its working fine but once the mouse gets close to the character the rotation becomes inaccurate, any ideas of why is that happening?
Thanks for that =D now its working good but once the mouse gets close to the character the rotating is not quit accurate to the mouse location.
any idea why is that happening?
Without seeing your code, I'm only guessing. $$anonymous$$y guess is that you did not use the correct value for 'z'. $$anonymous$$y use of 10 above was just an example.
my camera is 15 units away in the Z , i have changed the 10 was in the example to match my camera and tried bunch of other values,
but its still.. the problem only occurs when the mouse is near the arm im trying to rotate..
never $$anonymous$$d thanks a lot - i have tweaked something now it's all good <3 thanks a lot bro :D