This is the Script for Auto-Turrets i wrote and was demonstrated in one of my Videos.
Model Setup:The Bones (turret,Barrel,muzzle) should have their Pivots oriented like in Unity Y-up Z-Forward and should be linked as seen on the Screenshot:Body (v_chassis in my case) -turret -barrel -muzzlethe Barrel Mesh must be linked to the Muzzle Bone NOT to the Barrel bone for the Recoil Impulse to work. The Turret Script is looking for the closest GameObject with the 'Enemy'-Tag and when in Weapon Range only creates the given GameObject 'Ammo' each 'Ammo Switch Time' until 'Ammo Clip Count' runs empty then waits for 'Reload Time' seconds. With each created 'Ammo' the 'Muzzle' gets set 'Recoil Range'/1000 backwards (minus in Z-Axis) which goes each FixedUpdate 'Recoil Damping'/1000 Forwards until its Initial Position.The turret turns each fixed Update Time.deltaTime*'Turret Speed' onto the Target around its Y-Axis.
'Use Turret Limits' activates the respective Limit Values 'Min Turret Rotation' and 'Max Turret Rotation'The Barrel turns each fixed Update Time.deltaTime*'Barrel Speed' onto the Target around its X-Axis.
'Use Barrel Limits' activates the respective Limit Values 'Min Barrel Rotation' and 'Max Barrel Rotation'
The Parameters:
Muzzle:drag the Muzzle-Bone here as seen on the ScreenshotAmmo:drag the GameObject you want to be shot. Note the Force for the Projectile to fly is a Constant Force on the Ammo GameObject in my Case. Ammo Clip Count: an integer. Amount of Ammo to shoot in a row.Ammo Switch Time: time to wait before Shooting the next Ammo in the clipReload Time: time to wait after the Clip was empty before it gets refilledRecoil Range: the amount in 1/1000 the Muzzle bone gets set backwards in Z-Axis after each shotRecoil Damping: the amount in 1/1000 each FixedUpdate the Muzzle bone travels back forwardsTurret: drag the Turret-Bone here as seen on the ScreenshotTurret Speed: the Speed the turret Bone rotates towards the TargetUse Turret Limits: Activates Limitation of turret rotationMin Turret Rotation: turret can't rotate to smaller Values than this when 'Use Turret Limits' is enabled Max Turret Rotation: turret can't rotate to bigger Values than this when 'Use Turret Limits' is enabledBarrel: drag the Barrel-Bone here as seen on the ScreenshotBarrel Speed: the Speed the Barrel Bone rotates towards the TargetUse Barrel Limits: Activates Limitation of Barrel rotationMin Barrel Rotation: Barrel can't rotate to smaller Values than this when 'Use Barrel Limits' is enabledMax Barrel Rotation: Barrel can't rotate to bigger Values than this when 'Use Barrel Limits' is enabled
Thank you!