private GameObject GetTopTarget() { GameObject res = null; if (targetPriority == true) { float minDistance = float.MaxValue; foreach (GameObject obj in targets) { if (obj != null) { AIStatePatrol aiStatePatrol = obj.GetComponent <AIStatePatrol>(); float distance = aiStatePatrol.GetRemainingPath(); if (distance < minDistance) { minDistance = distance; res = obj; } } } } else { res = targets[0]; } targets.Clear(); return(res); }
private void Start() { // Getting cache variables _navMeshAgent = GetComponent <NavMeshAgent>(); _animator = GetComponent <Animator>(); _mouthAudioSource = GetComponent <AudioSource>(); _targetTrigger = GetComponentInChildren <TargetTrigger>(); AIDecapitation[] parts = GetComponentsInChildren <AIDecapitation>(); foreach (AIDecapitation part in parts) { _parts.Add(part); } HandleAnimatorController(); // Getting the skinned mesh renderers for the metamorphosis if (_metamorphosisMaterial != null) { SkinnedMeshRenderer[] skinnedMeshRenderers = GetComponentsInChildren <SkinnedMeshRenderer>(); foreach (SkinnedMeshRenderer meshRenderer in skinnedMeshRenderers) { _skinnedMeshRenderers.Add(meshRenderer); } } // We always have the rotation of the navmesh deactivated! // In fact, we either rotate with the root motion rotation of the turn around animation // Or we rotate it manually through code. // In sum, the navmesh is only useful for calculating the path: steertingTarget :) if (_navMeshAgent != null) { _navMeshAgent.updateRotation = false; } if (_targetTrigger != null) { _targetTrigger.RegisterTargetTrigger(this); } // Storing states AIStateIdle stateIdle = GetComponent <AIStateIdle>(); if (stateIdle != null) { _statesDictionary.Add(AIStateType.Idle, stateIdle); _statesDictionary[AIStateType.Idle].RegisterState(this); } AIStatePatrol statePatrol = GetComponent <AIStatePatrol>(); if (statePatrol != null) { _statesDictionary.Add(AIStateType.Patrol, statePatrol); _statesDictionary[AIStateType.Patrol].RegisterState(this); } AIStatePursuit statePursuit = GetComponent <AIStatePursuit>(); if (statePursuit != null) { _statesDictionary.Add(AIStateType.Pursuit, statePursuit); _statesDictionary[AIStateType.Pursuit].RegisterState(this); } AIStateAttack stateAttack = GetComponent <AIStateAttack>(); if (stateAttack != null) { _statesDictionary.Add(AIStateType.Attacking, stateAttack); _statesDictionary[AIStateType.Attacking].RegisterState(this); } AIStateAlert stateAlert = GetComponent <AIStateAlert>(); if (stateAlert != null) { _statesDictionary.Add(AIStateType.Alert, stateAlert); _statesDictionary[AIStateType.Alert].RegisterState(this); } if (_statesDictionary.ContainsKey(AIStateType.Idle) && _statesDictionary[AIStateType.Idle] != null) { ChangeState(AIStateType.Idle); } // Get all body parts snapshot Transform[] bodyParts = transform.GetComponentsInChildren <Transform>(); foreach (Transform bodyPart in bodyParts) { BodyPartSnapShot bodyPartSnapshot = new BodyPartSnapShot(); bodyPartSnapshot.transform = bodyPart; _bodyPartsSnapshots.Add(bodyPartSnapshot); } // At the beginning, we should always be able to scream _screamTimer = _screamDelay; // Getting the navmesh agent initial speed for damageBehavior (in case we aren't using root rotation) if (_navMeshAgent != null) { _initialSpeed = _navMeshAgent.speed; } _initialVehicleStoppingDistance = _vehicleStoppingDistance; }