I’m trying to run an auto shooting script once lockedTarget is true after the turret has completed it’s rotation and is looking towards the enemy target.
public class LookAtEnemy : MonoBehaviour
{
// The enemy target.
[ReadOnly] public GameObject enemyTarget;
// The turret that will rotate and point towards an enemy target.
public Transform turretTransform;
// Smoothly rotate the turret towards the enemy target.
public float rotateSpeed = 6f;
// After smoothly rotating, is the turret pointing towards the enemy target now?
[ReadOnly] public bool lockedTarget;
private void FixedUpdate()
{
// Get an enemy target.
enemyTarget = CommonMethods.FindClosestTargetTag(transform, "Enemy", false);
// Rotate Lil John (turret) to point towards the enemy.
if (turretTransform != null)
{
// Direction of the enemy target normalized.
Vector3 directionToTargetNormalized = (enemyTarget.transform.position - turretTransform.position).normalized;
// Desired rotation to rotate towards so that the turret is facing the enemy target.
Vector3 desiredRotation = Vector3.RotateTowards(turretTransform.forward, directionToTargetNormalized, rotateSpeed * Time.fixedDeltaTime, 0f);
// Make sure it does not rotate up/down.
desiredRotation.y = 0f;
// Now rotate Lil John!~
turretTransform.rotation = (Quaternion.LookRotation(desiredRotation));
}
}
}