Gun Turret
Using Unity 2021.3.27f1 and Visual Scripting 1.8.0. The project is using the 2D Core template.
Download the graph: GunTurret.asset
The gun turret is composed of a few parts that needs explanation for the script to work properly.
The Gun Turret
game object, is just an empty game object.
The Head
is a child of the Gun Turret
and should be facing in its local Z direction (Unity’s forward).
The Bullet
should be a child of the Head
and should also be facing forward - local Z. The bullet has a Collider
and a Rigidbody
, since we will let the physics simulation handle the bullet once it is fired. The mass of the bullet is higher than the cubes, to make a bigger impact.
Note: The rotation of the head is not limited in any way. This means it can potentially shoot straight up or down.
In addition, the script uses a Layer Mask
to filter which game objects it detects. In this example, the stacked cubes are set to the Player
layer (you need to add this yourself). You can use any layer you would like, just be sure to also set it in the Graph variables.