- Home /
[2D] Player's arm following the mouse
So as the titles says I want the players arm to pivot on his shoulder so the hand points towards the mouse. LookAt doesn't work because it tries to change the z-Axis which makes the sprite disappear I've tried other methods but can't quite figure it out.
Answer by etopsirhc · Jul 30, 2015 at 07:44 PM
a work around would be to after using look at move the value from z to the proper one. but that may still have issues with being the right angle. best way i know of would be to calculate the angle needed yourself. you have the x/y pos of the mouse as one point, and the x/y of the arm as a second. the straight line between the 2 points would be the hypotenuse of a right triangle, from there it's just a little bit of trig to get what you want.
Ok I'm still in highschool and I haven't learnt trigonometry yet but I think the equation in code is angle = $$anonymous$$athf.asin( opposite / hypoteneuse) is that right?
close, you have the opposite and adjacent already, the x and y distances between the mouse and pivot point. so the code would be float angle = $$anonymous$$athf.Atan(opposite/adjacent);
to help with trig later, soh cah toa sine(op/hyp) cosine(adj/hyp) tangent(op/adj)
It's been a while but that all works with the math but the mouse position doesn't work right how do you get the position properly.