Search Unity

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

    kaydenfinley

    Joined:
    Mar 12, 2019
    Posts:
    3
    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

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    4,087
    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.