void Update() { switch (state) { default: case State.Roaming: AIMovement.MoveTo(roamPosition, ship, dampingOnTurning); Debug.DrawLine(transform.position, roamPosition, Color.green); float reachedPositionDistance = 10f; if (Vector3.Distance(transform.position, roamPosition) < reachedPositionDistance) { // Reached roam position, thus getting new roam position roamPosition = AIMovement.GetRoamingPosition(transform.position); } GameObject player = GameObject.FindGameObjectWithTag("PlayerTag"); FindTarget(player); break; case State.ChaseTarget: // Look at player // If within shootingRange of player, stop and shoot at it // Otherwise move towards player // TODO: Think of a better way to calculate accuracy, also use player's speed in calculation. // Choose a random position around the player where AI will shoot. Vector3 targetPosition = Random.insideUnitSphere * (35 * (1 / shootingAccuracy)) + currentTarget.transform.position; var rotation = Quaternion.LookRotation(targetPosition - transform.position); transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * dampingOnTurning); if (Vector3.Distance(targetPosition, transform.position) < shootingRange) { ship.Stabilise(); weaponSystem.ShootGuns(ship.currentVelocity); } else { Debug.DrawLine(transform.position, targetPosition, Color.green); AIMovement.MoveTo(targetPosition, ship, dampingOnTurning); } break; } }
void Start() { roamPosition = AIMovement.GetRoamingPosition(transform.position); }