This problem is really confusing me and driving me nuts. Originally I had a simple “shoot” script attached to my player object that would shoot a single bullet at the mouse cursor, and it worked fine. Here’s what it looked like:
I decided to make things a little more interesting and add objects that would circle around the player and also serve as the sources of bullets rather than the player. The idea would be that each of these shooter objects shoots directly at the cursor, each basically acting exactly like the player object did before. Right now I have four set up, but the number should be irrelevant; the end effect would be a group of bullets that converge on the mouse cursor when fired. Here’s what the code looks like:
I’m getting strange results. At first, the bullets appeared to spray out in random directions. After playing with it a bit, I realized something peculiar was happening. it’s as though the mouse position is now registering as being in the correct direction relative to the player or center of gravity of the four shooter objects, but also registering as being way closer to that center than it actually is. I made a crappy picture to try to illustrate this:
You can ignore that sphere below the player… that was just an object I put in so I would have something to judge the player’s speed by when I was implementing movement.
So… I messed around with the code a bit. Tried replacing various occurrences of “transform.position” with “transform.TransformDirection(transform.direction)” as well as using InverseTransformDirection, thinking that the fact that the shooter objects were children of the player was causing them to represent there values in local rather than world space. Every change I made only made things worse.
Trying to dissect this even more, I un-parented the shooter objects from the player, removed the code from the player object, and attached it to all four shooters separately in addition to converting it back to the original one-bullet code that appeared at the beginning of this post - the one that simply shoots one bullet from the player to the mouse cursor. I expected this should work fine, as it would seem it should be doing the exact same thing as when it worked, only four separate times from four separate positions in space. I am now losing my mind, because the same thing is still happening, and I can’t for the life of me think of any logical reason why this should be happening. If I also add the original code back to the player object, it will shoot a bullet straight at the cursor as intended.
I must be missing something incredibly obvious that I’ll feel stupid about when somebody points it out to me… So somebody, please… make me feel stupid before I go insane.
I just noticed something interesting… I tried moving the player relative to the camera so that it’s not in the center of the screen anymore (like the shooter objects). It no longer shoots in the correct direction. I still don’t understand WHY this is happening, though.
I fixed the problem. I’m not sure if I’m proud of myself or feel like an even bigger idiot…
My mistake was that I misunderstood how ScreenToWorldPoint() works. I set the z value of the vector returned by Input.MousePosition() to an arbitrary value (in this case 1) because I read somewhere that the you have to set it to some value before ScreenToWorldPoint() could do anything with it. While this was true, I did not realize that STWP uses that value to determine what plane in front of the camera to project the screen point onto. Because I set the value to 1, I was finding the x and y coordinates of the mouse cursor one unit in front of the camera. The player is obviously much further from the camera than one unit.
I don’t know if I should delete this thread or leave it up in case anyone else runs into the same issue?