Controlling the turret
To integrate EasyTurret into your project, you'll need a script to designate targets and coordinate with your weapon. Usually, this simply involves using another script to invoke various methods on the turret. However, EasyTurret can also be configured to send events to another script using the interface ITurretEvents. Interface method described below. For complete details, see MF_EasyTurret script reference. To properly aim to hit a target, the turret needs the shot speed of the weapon. (Except direct aim mode does not use shot speed.) This can be entered manually or set via script. For the turret to compute intercept, you'll also need velocity data of the shooter and target. To access MF_EasyTurret and other classes, add at the top of your script: using MobFarm; Set a field to reference an MF_EasyTurret script: MF_EasyTurret turretScript; Then in Awake() cache a reference to the turret script of the turret you wish to access: void Awake ( ) { turretScript = myTurret.GetComponent<MF_EasyTurret>(); } Use turret methods thus: turretScript.SetTarget( target, targetRigidbody ); Some methods to get you started: SetTarget() Set the turret's target: turretScript.SetTarget( target, targetRigidbody ) turretScript.SetTarget( target, targetRigidbody2D ) target: the transform of the target. Set to null to clear a target. targetRigidbody: The rigidbody of the target. This is used to find the target's velocity data for intercept calculations. Without a rigidbody, velocity calculations are much less accurate. GetTarget() Returns the transform of the current target: turretScript.GetTarget() PositionWithinLimits() Bool test if a position is within the turret's gimbal limits: turretScript.PositionWithinLimits( position ) position: the vector3 location of the position to test. This can be used to choose if a particular target should be passed to the turret. AimCheck() Bool test if turret is aimed to hit its current target, if any: turretScript.AimCheck( aimTolerance ) aimTolerance: This angle is how close (in degrees) to the direction to the target location necessary to return a true result. Avoid using 0 due to floating point errors. This can be used to decide when to fire a weapon. Using interface ITurretEvents
If you want EasyTurret to send TurretEvents to other scripts, you can use use an interface. You'll first need to set up your script to handle sent events, then add it to a turret's list of items to send events to. Set up your class to inherit the interface: public class MyScript : Monobehaviour, ITurretEvents Then implement the TurretEvent() method: public void TurretEvent ( TurretEventType eventType ) { } In this method you can check for any or all particular events of TurretEventType: enum TurretEventType { AimedAtTarget, GainedTargetAim, LostTargetAim, GainedTarget, LostTarget } AimedAtTarget: Sends this TurretEventType every frame the turret is aimed to hit its current target. (within 0.5 degrees) GainedTargetAim: Sends this event upon reaching the aim to hit its target. Turret must loose proper aim before sending this event again. LostTargetAim: Sent upon loosing proper aim to the target. Turret must gain proper aim before sending this event again. GainedTarget: Sent upon gaining a new target. LostTarget: Sent upon loosing a target. For example, to have some action upon the turret gaining the proper aim to hit its target: public void TurretEvent ( eventType ) { if ( eventType == TurrtEventType.GainedTargetAim ) { // do a thing here } } Finally, in the inspector of the script with an MF_EasyTurret script, drag and drop the object with the script inheriting ITurretEvents into the list of Event Targets. Upon Awake() the turret will assign these items as places to send TurretEvents. Alternately, you can use: AddEventTarget ( eventTarget ) RemoveEventTarget ( eventTarget ) to add or remove an object during runtime. |
Set-up Tutorial
Setting up your own turret
If you have a turret asset that already has a proper hierarchy for rotation and elevation, set-up will be easy.
A blank turret prefab with the proper hierarchy has been provided to make things easier.
Next, we'll add mesh objects to represent the turret parts. Instead of placing mesh components directly to the parts, it is highly recommended to give these their own GameObjects placed as children of the part. This will allow you to freely move, scale, or rotate the mesh object.
|