So, I’m working on a 3d side scroller, wherin the character shoots at things on screen. I’m using this tutorial for the base setup. However, I’m modifying it so that instead of aiming at mouse positions, it instead aims at the location of a raycast impact, instead of the mouse position. To do this i’ve made an invisible plane that only the raycasts can hit, with them originating from the camera position.
I’ve got it figured out so far that it works perfectly to the right of the character, however, when the impacts are to the left of the character, they aim at the opposite direction, as though the impact were mirrored on the opposite side. Using printstrings I’ve figured out that the rotation of the sphere at 9:00 and 3:00 is 0 degrees, 45 degrees at 10:30 and 1:30, and 90 degrees at 12:00. Then at 4:30 and 7:30 its -45 degrees, and 6:00 its -90.
How can I get it to work on both sides? Is there a way I can rotate that 0 point vertically?
Essentially it would seem find look at rotation is for screen co-ordinate comparison, and not world coordinate comparison. So, instead of using that function, you gotta do your own function to find the rotation in worldspace. For that we had to compare the two vectors (-) and then invert the horizontal axis (in my case, the y axis) before normalizing it. Then using sign(sin(y)) we are able to determine if the impact is to the left(-) or right(+) side of the character. We then multiply that result by cos(z) to get the resulting angle …somehow. I’m not a mathematician! and finally I had to multiply that by -1 because my bolts were coming out in the opposite direction of the impact point, and then I’d add 90 to make sure that the 0 point of the shootsphere and the 0 point of the rotation comparison were the same.