Multiple ways to make a projectile move from a gun with gravity.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Jai

I am coding a game with a gun and need it to have gravity. I would love people to give as many ways to do this to see what looks and plays the best for my game. Thanks.

:bust_in_silhouette: Reply From: godot_dev_

##Preliminary info

Before I address your question, here is a bit of information on gun hitboxes in shooter games. To my knowledge, in first-person-shooters and I imagine in other shooting games as well, there are two types of guns: a) hitscan, and b) projectile.

  • Hitscan guns immediacy have a hitbox when you shoot, like a sniper rifle or a energy beam (e.g., the sentinel gun from Halo or Zarya’s weapon from Overwatch) . Wherever the player is aiming, the frame the gun shoots it will connect with anything in the line of fire.
  • You can acheive this using Raycasts that exit from the gun (or the center of the player’s Camera) and point to where the enemy is aiming
  • Projectile guns shoot a projectile and there is travel time to the bullet. The projectile/bullet may have no gravity (a slow stream of bullets for example) or it may have gravity (like an arrow) so you have to lead your shot upward to take into consideration the gravity when aiming
  • You can achieve this by having the bullet scene be independent from the gun scene. You can use KinematicBody2D, for example (assuming a 2D game), where the bullet travels and has a hitbox (Area2D with CollisionShape2Ds). Projectile bullets become more complicated because high speed bullets may lead to collision issues (see thispost)

##Answering Your Question
You mentioned the gun has gravity. I assume you mean the bullets are affected by gravity. This means you probably want to go with the projectile gun design. I have seen graphical tricks where a hitscan weapon appears to be projectile-based by including a quick animation of travel bullets spray while the hitbox (the RayCast) is unrelated to the sprite animation (Overwatch does this trick with heros like Tracer, where you see the bullet spray, but the spray is only visual). If you want your bullet to appear like it has gravity and is extremely quick, you may be able to get away with this RayCast + sprite animation trick by adding a curved RayCast if such a thing is possible (e.g., by having multiple Rayscasts to form a curve).
If you meant that you want a gravity gun (as opposed to bullets with gravity), I beleive the same logic applies for shooting the gravity bullets. You will just need some extra thought into how gravity of a victim shot by your gravity gun is affected by different gravity

Thanks, I meant I am making a gun in 3D with bullets that need to be affected by gravity. I tried using the translate function however it did not work with my collisions, as I want the bullets to hit and move rigid bodies. I also tried the apply_impulse function which also did not work as I couldn’t get the bullet to be affected by gravity using as it was adding constant velocity.

Thanks for the help anyways.

Jai | 2023-05-05 00:06