- Home /
FPS aim is slightly off center - using ScreenPointToRay
I am making an fps game but my shots always end up slightly below and to the right of where they are supposed to hit (whatever is in the center of the screen.) I've used the ScreenPointToRay function to aim the gun at whatever point is currently displayed in the center of the screen. Problem is, when I actually fire the gun the projectile ends up the slightest bit off of where it should be. See here:
That is where the shot hits the wall (I've tested enough to know it's not just me moving the reticle after firing.)
I've tried everything I can think of -- I used a debug raycast to make sure the shot spawner on the end of the gun is rotated to face the target point; I've double checked that the raycast points toward the center -- the point at which the gun should be aiming is definitely correct. Could it be possible the bullet object itself is offset from its origin somehow? It doesn't look like it in the inspector but I really don't know. Please help, I'm running low on ideas!
Does the error reduce over a larger distance?
Are you taking in to account the difference in position between the centre of your camera and the gun?
Yes, the error does reduce over a larger distance. So I might not be taking into account the difference in position, but I use a LookAt function to point the bullet spawn object at the hit point of the ScreenPointToRay, so I would think that would make it always face exactly at that point.
I realized it's possible the error is caused by the bullet spawn point (which is invisible) not actually rotating to face the aim point, resulting in the gun just firing straight forward. But I'm not sure how that would occur unless I'm somehow using the LookAt function incorrectly.
Answer by Agent000 · Mar 09, 2015 at 09:57 PM
Haha, it was something stupid I missed. Knew it had to be something like this. My script was referencing the wrong object's rotation -- I had it attached to the gun object so it was referencing the gun's rotation not the BulletSpawn's rotation. Fixed, working fine now. Thanks for the help :)
Answer by Enemby · Mar 09, 2015 at 02:22 PM
First off, why are you spawning the bullet spawn point from the actual gun? I think that is probably the cause of your problem, since gun isn't centered on the camera's center (transform wise, that is)
You don't need to spawn the bullet at the gun's location, especially in the case of a raycast weapon.
If the bullet is a raycast and not a gameobject this approach may make sense. However when you have a guy running round with bullets co$$anonymous$$g out of his face, it doesn't really hold up. Its best to calculate the difference. The latter is the approach they use in most games. You can see this when you fire at a wall or something at different distances. Stand right up against an object and shoot and you can see which method they've used.
What would I spawn them from if not the gun? I'm not literally spawning the bullets from the actual gun model, I have an invisible gameobject BulletSpawn childed to the end of the gun model, and this BulletSpawn object uses a LookAt function to face the raycast hit point. Then the bullet is supposed to spawn with the same rotation and position as the BulletSpawn object, and move in its forward direction. If either the LookAt function or setting the bullet's transform isn't working for whatever reason, that would cause the bullet to just fire straight out of the gun, which could cause the problem. I'm going to check whether the BulletSpawn object is actually rotating like it's supposed to -- I'll get back to you once I've done that.
Your answer
Follow this Question
Related Questions
Why does this script I got from the Unity Document site does not work? 1 Answer
Hit distance Change light range 1 Answer
Aiming Down Sights Problem 2 Answers
Shooting script no working correctly 1 Answer
Raycast Shooting 1 Answer