Search Unity

Best practice when shooting in a third person game

Discussion in 'Scripting' started by sujitha304, Jun 10, 2019.

  1. sujitha304

    sujitha304

    Joined:
    Apr 15, 2018
    Posts:
    5
    Hey there guys im working on a third person over the shoulder shooter game and im stuck on how you would implement the shooting aspect.Whats the best practice to do this. should i
    • instantiate a bullet that goes towards the cross hairs direction - but when i do this i have to compromise on the z direction, because depending on the distance the bullets wont go through the cross hair
    • should i ray cast and ditch the instantiation and just show a muzzle flash and take care of the hit some other form
    to understand more of this i played gears as its quite similar, in that you can see bullets going towards the cross hair no matter the distance,

    And if what Im saying doesn't make any sense could you point me towards the direction, what i should look at
    any help is appreciated :):):)
     
    Last edited: Jun 10, 2019
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Either works, it just depends. With the former option, you get physical projectiles that players can actually sort of see, but you could get this with special effects as well (for instance, 2D billboards) which may be cheaper than doing physics calculations. You'll probably have to raycast in the former case anyways (forward from the position of the bullet every frame) if you use really fast projectiles, since they can go from one side of an object to the other in a single frame without actually making "contact" otherwise, I believe. You'd get a realistic delay if the bullets aren't lightning-fast with object models/colliders though, allowing for some 'close call' dodges that would look awesome in instant-replays.

    Either way, you shouldn't be instantiating bullets when they're fired- if you use the physical projectile option, you should absolutely be using a GameObject pooling method. That way, you can instantiate like a hundred when the game first loads, deactivate them and put them in a collection somewhere, then when a weapon fires, it can pull an instance of the bullet from there, "fire" it, then when it hits something, return it to the pool again without destroying it. As much as possible, avoid instantiation and destruction of GameObjects in the middle of scenes.

    The rest is primarily just a style choice, do whatever seems the most fun. =)
     
  3. sujitha304

    sujitha304

    Joined:
    Apr 15, 2018
    Posts:
    5
    Hey thanks for the quick reply after reading what you said I'm going to go ahead with physical bullets way , it just looks cooler ^_^