Hi, i’ve looked at multiple tutorials at looking at mouse to rotate, but when i rotate, there is an offset since the gun is on the side of the player. So when my cursor is on an enemy, the bullets arent hitting him since the offset. Any ideas how to fix this?
So, since you didn’t post any code I can’t really help with your code but I can tell you how to do it.
There are two ways I can think of but it depends on how your bullets work.
Basically you are making a right triangle with points player, gun, mousePointer. So all you need to do is find angle theta which is the angle between the bullet trajectory and the vector starting at player and ending at gun.
X we will call the distance from the gun to the player, this is a constant value which you can easily find by looking at the child transform. Z we will call the distance from the player to enemy. There are several ways you can get the Z distance and if you need help just ask. One simple way is just to subtract your mousPointer.position.z from your player.position.z
Then just use theta = Mathf.Tan(Z/X) * Mathf.Rad2Deg. This will give you the angle in degrees. Then you have to say angle = 90-theta; because we want the angle of the vector that is parallel to Z.
Then simply rotate your bulletSpawn point by that angle before applying a force to the bullet.
You will have to check all these values in an update function but don’t have to update them every frame. You only need to update if distance Z changes by certain percentage which is dependent on your setup.
You could parent an empty to where the gun is, just where the bullet leaves. Then rotate the player first, and the empty next, and have the bullet follow the empty rotation.
Yes, this is what I explained in detail. He will still have to calculate the angle theta to determine how much to rotate bulletSpawn/empty gameObject.
He can just use transform.LookAt on the empty to the enemy position. Then just instantiate the bullet at that rotation.
Sorry, I forgot to mention that the shooting uses raycasting rather than spawning prefabs. I didnt post a code because I have started but what I came up with is to have the ray shoot out from the gun. The problem is my cursor and the location of the ray is offset (imagine holding a rifle straddled in your right arm, the players center is in line with the cursor)