First of all, create the prefab of the necessary turret parts from the prefabs folder.
Result of combining prefabs
Next, you should add the following scripts to the created prefab:
Turret
- main script which controls the turret rotation settings and fire tick variable.Target Finder
- script to find possible targets to start attacking.Firing Event
script - main script to determine how the turret fires.In the Turret
script, we pass a game object that will rotate:
Also we pass the projectile prefab as reference of a game object.
In the Target Finder script, we pass a game object that we use as a pivot to find targets:
By default, target tag for a game object we want to target is Enemy.
In the child of Firing Event
script, we pass a turret gun game object. For example, we use Simple Fire
script:
Next, you should add the Gun script to the turret gun prefab. It holds the muzzle object to determine transform position for instantiate projectile prefab:
In other cases, you probably need to add some kind of animations - fire or barrel rotation scripts.