- Home /
Prevent bullets from trailing behind crosshair at high range?
I'm making a first-person shooter. I have a gun that casts a ray to check where the bullet will hit and calculates accordingly to make sure the bullet actually hits the crosshair (not straight out from the gun, which would hit below and to the right of the crosshair). I have a bullet gameobject that is spawned, which simply goes from the gun to the pre-calculated crosshair hit point. The bullet does nothing but travel there at a high speed (100 units per second.) However, when shooting at high range the bullets appear to trail behind the crosshair when looking around and shooting, which is not the effect I want to achieve. Suggestions on how to fix this?
Answer by Acdia · Mar 23 at 08:44 AM
Hey! I think the question is, what effect you want then, since a physical bullet normally will travel in a straight line after being shot (even in real live). I think there are several options to get the bullet hit accurately.
Speed - 100 Units per second is, compared to a real live bullet, rather slow (~800 m/s). So you could just speed up the bullet even more. The downside is, that collision detection gets a real pain at this speed - which probably doesn't matter, if you use a raycast to check the collision before even shooting the projectile. But then again, if you want to hit accurately and don't care about gravity, you could get rid of the bullet:
No bullet - Instead of using an actual projectile, you can just use some effect to give the illusion of a bullet flying (at 800 m/s you probably wouldn't see anything, but some visuals are often wanted). You could, for instance, use a line renderer. Just set the start and end point to the pistol tip and the hit point and put some animation on it, that it quickly flashes. Make sure to update the start position to the pistol, while the animation is playing. This way the line will be correctly rendered and isn't flying away.
home-seeking-missile-ish-crap - You can give a pointer to the playerObject to the bullet and let it read the current hit location (perform raycast every Update). Then subtract the bullet position from the hitPoint position, normalize the vector, multiply it with the desired speed and set it as the velocity of the bullet. This way the bullet should always fly towards the crosshair. Despite making no sense, since the bullet now can fly in an arc, whis probably isn't what you want, it could look pretty fun - maybe a new idea for a game mechanic?
Anyway, I would go with the second approach - I hope this contains something useful.