Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

shooting based off of where the head/end of the barrel is rotated on a 2d axis in a 3d game

Discussion in 'Scripting' started by kaydenfinley, Aug 3, 2019.

  1. kaydenfinley


    Mar 12, 2019
    hey im still a new coder and decided to go my own path and make games from what i know (extremly basic code) but i need to shoot bullets from a spawner at the end of my turrets barrel, the barrel rotates left and right when holding a key so what code do i add to make it shoot?
  2. Kurt-Dekker


    Mar 16, 2013
    Generally you make a prefab of the moving parts of your gunbase, or just make an entire scene.

    You would parent the gun barrel to the turret pivot, etc.

    Then you stick a GameObject (also parented) where you want the muzzle to spawn, and point that GameObject so that its +Z axis (blue) is the bullet direction.

    Now just use that GameObject (or technically its Transform) to know where and which direction to shoot.