public override void HandleAnimator() { _stateMachine.Animator.SetBool("IsTurningRight", _turningRight); _stateMachine.Animator.SetBool("IsTurningLeft", _turningLeft); bool alertLayerActivated = false; if (_stateMachine.CurrentTarget != null) { AITargetType targetType = _stateMachine.CurrentTarget.Type; if (targetType == AITargetType.BoomBox || targetType == AITargetType.Friend || targetType == AITargetType.NavigationPoint || targetType == AITargetType.None) { alertLayerActivated = false; } if (targetType == AITargetType.Enemy || targetType == AITargetType.Player || targetType == AITargetType.Sound) { alertLayerActivated = true; } } _stateMachine.Animator.SetBool("Alert", alertLayerActivated); }
public override void HandleAnimator() { int animatorSpeedToSet = _stateMachine.Agent.desiredVelocity.magnitude > 0 ? 1 : 0; if (_stateMachine.CurrentTarget != null && _stateMachine.CurrentTarget.Type == AITargetType.Friend && !_shouldMakeAMove) { animatorSpeedToSet = 0; } _stateMachine.Animator.SetInteger("Speed", animatorSpeedToSet); bool alertLayerActivated = false; if (_stateMachine.CurrentTarget != null) { AITargetType targetType = _stateMachine.CurrentTarget.Type; if (targetType == AITargetType.BoomBox || targetType == AITargetType.Friend || targetType == AITargetType.NavigationPoint || targetType == AITargetType.None) { alertLayerActivated = false; } if (targetType == AITargetType.Enemy || targetType == AITargetType.Player || targetType == AITargetType.Sound) { alertLayerActivated = true; } } _stateMachine.Animator.SetBool("Alert", alertLayerActivated); }
/// <summary> /// Set a target that has no collider /// </summary> /// <param name="t"></param> /// <param name="p"> position </param> /// <param name="d"> distance to target </param> public void Set(AITargetType t, Vector3 p, float d) { _type = t; _position = p; _distance = d; _time = Time.time; }
public void Clear() { _type = AITargetType.None; _collider = null; _position = Vector3.zero; _time = 0.0f; _distance = Mathf.Infinity; }
public void Clear() { this._type = AITargetType.None; this._collider = null; this._position = Vector3.zero; this._distance = Mathf.Infinity; this._time = 0; }
public void Set(AITargetType t, Collider c, Vector3 p, float d) { this._type = t; this._collider = c; this._position = p; this._distance = d; this._time = UnityEngine.Time.time; }
public void Set(AITargetType type, Collider collider, Vector3 position, float distance) { _type = type; _collider = collider; _position = position; _distance = distance; _time = Time.time; }
// Sets the target and moves the target trigger to that location public void SetTarget(AITargetType t, Collider c, Vector3 p, float d, float s) { _target.Set(t, c, p, d); if (_targetTrigger != null) { _targetTrigger.radius = s; _targetTrigger.transform.position = _target.position; _targetTrigger.enabled = true; } }
public void SetTarget(Transform transform, AITargetType targetType) { AITarget target = new AITarget(); target.TargetTransform = transform; target.LastSeenPosition = transform.position + Vector3.up; target.Type = targetType; _currentTarget = target; }
public void SetTarget(AITargetType t, Collider c, Vector3 v, float d) { _target.Set(t, c, v, d); if (_targetTrigger != null) { _targetTrigger.radius = _stoppingDistance; _targetTrigger.transform.position = _target.position; _targetTrigger.enabled = true; } }
// Consente di specificare una stopping distance personalizzata. public void SetTarget(AITargetType t, Collider c, Vector3 p, float d, float s) { _target.Set(t, c, p, d); // Configura e abilita il target trigger alla posizione e raggio corretti if (_targetTrigger != null) { _targetTrigger.radius = s; _targetTrigger.transform.position = _target.position; _targetTrigger.enabled = true; } }
public override void OnTriggerEvent(AITriggerEventType eventType, Collider other) { if (_npcStateMachine == null) { return; } if (eventType != AITriggerEventType.Exit) { AITargetType curType = _npcStateMachine.VisualThreat.type; if (other.CompareTag("Player")) { float distance = Vector3.Distance(_npcStateMachine.sensorPosition, other.transform.position); if (curType != AITargetType.Visual_Player || (curType == AITargetType.Visual_Player && distance < _npcStateMachine.VisualThreat.distance)) { RaycastHit hitInfo; if (ColliderIsVisible(other, out hitInfo, _playerLayerMask)) { _npcStateMachine.VisualThreat.Set(AITargetType.Visual_Player, other, other.transform.position, distance); } } } else if (other.CompareTag("AI Sound Emitter")) { SphereCollider soundTrigger = (SphereCollider)other; if (soundTrigger == null) { return; } Vector3 agentSensorPosition = _npcStateMachine.sensorPosition; Vector3 soundPos; float soundRadius; AIState.ConvertSphereColliderToWorldSpace(soundTrigger, out soundPos, out soundRadius); float distanceToThreat = (soundPos - agentSensorPosition).magnitude; float distanceFactor = (distanceToThreat / soundRadius); // Too far away if (distanceFactor > 1.0f) { return; } if (distanceToThreat < _npcStateMachine.AudioThreat.distance) { _npcStateMachine.AudioThreat.Set(AITargetType.Audio, other, soundPos, distanceToThreat); } } } }
// -------------------------------------------------------------------- // Name : SetTarget (Overload) // Desc : Sets the current target and configures the target trigger. // This method allows for specifying a custom stopping // distance. // -------------------------------------------------------------------- public void SetTarget(AITargetType t, Collider c, Vector3 p, float d, float s) { // Set the target Data _target.Set(t, c, p, d); // Configure and enable the target trigger at the correct // position and with the correct radius if (_targetTrigger != null) { _targetTrigger.radius = s; _targetTrigger.transform.position = _target.position; _targetTrigger.enabled = true; } }
/// <summary> /// Set this items target to something /// </summary> /// <param name="t"></param> /// <param name="p"> The position of the target in 3D space </param> /// <param name="d"> The distance to the target </param> /// <param name="c"> The collider on the target if it has one: this can be null </param> public void SetTarget(AITargetType t, Vector3 p, float d, Collider c = null) { if (Logging && !targetSet) { Debug.Log($"The target for {gameObject.name} has been set"); targetSet = true; } if (c == null) { Target.Set(t, p, d); } else { Target.Set(t, c, p, d); } }
public override void OnTriggerEvent(AITriggerEventType eventType, Collider other) { //base.OnTriggerEvent(eventType, other); if (_zombieStateMachine == null) { return; } if (eventType != AITriggerEventType.Exit) { AITargetType curType = _zombieStateMachine.VisualThreat.type; if (other.CompareTag("Player")) { float distance = Vector3.Distance(_zombieStateMachine.sensorPosition, other.transform.position); if (curType != AITargetType.Visual_Player || (curType == AITargetType.Visual_Player && distance < _zombieStateMachine.VisualThreat.distance)) { RaycastHit hitInfo; if (ColliderIsVisible(other, out hitInfo, _playerLayerMask)) { _zombieStateMachine.VisualThreat.Set(AITargetType.Visual_Player, other, other.transform.position, distance); } } } else if (other.CompareTag("Flash Light") && curType != AITargetType.Visual_Player) { BoxCollider flashLightTrigger = (BoxCollider)other; float distanceToThreat = Vector3.Distance(_zombieStateMachine.sensorPosition, flashLightTrigger.transform.position); float zSize = flashLightTrigger.size.z * flashLightTrigger.transform.lossyScale.z; float aggrFactor = distanceToThreat / zSize; if (aggrFactor < _zombieStateMachine.sight && aggrFactor <= _zombieStateMachine.intelligence) { _zombieStateMachine.VisualThreat.Set(AITargetType.Visual_Light, other, other.transform.position, distanceToThreat); } } else if (other.CompareTag("AI Sound Emitter")) { SphereCollider soundTrigger = (SphereCollider)other; if (soundTrigger == null) { return; } Vector3 agentSensorPosition = _zombieStateMachine.sensorPosition; Vector3 soundPos; float soundRadius; AIState.ConvertSphereColliderToWorldSpace(soundTrigger, out soundPos, out soundRadius); float distanceToThreat = (soundPos - agentSensorPosition).magnitude; float distanceFactor = (distanceToThreat / soundRadius); distanceFactor += distanceFactor * (1.0f - _zombieStateMachine.hearing); if (distanceFactor > 1.0f) { return; } if (distanceToThreat < _zombieStateMachine.AudioThreat.distance) { _zombieStateMachine.AudioThreat.Set(AITargetType.Audio, other, soundPos, distanceToThreat); } } else if (other.CompareTag("AI Food") && curType != AITargetType.Visual_Player && curType != AITargetType.Visual_Light && _zombieStateMachine.satisfaction <= 0.9f && _zombieStateMachine.AudioThreat.type == AITargetType.None) { float distanceToThreat = Vector3.Distance(other.transform.position, _zombieStateMachine.sensorPosition); if (distanceToThreat < _zombieStateMachine.VisualThreat.distance) { RaycastHit hitInfo; if (ColliderIsVisible(other, out hitInfo, _visualLayerMask)) { _zombieStateMachine.VisualThreat.Set(AITargetType.Visual_Food, other, other.transform.position, distanceToThreat); } } } } }
// ------------------------------------------------------------------------------------- // Name : OnTriggerEvent // Desc : Called by the parent state machine when threats enter/stay/exit the zombie's // sensor trigger, This will be any colliders assigned to the Visual or Audio // Aggravator layers or the player. // It examines the threat and stored it in the parent machine Visual or Audio // threat members if found to be a higher priority threat. // -------------------------------------------------------------------------------------- public override void OnTriggerEvent(AITriggerEventType eventType, Collider other) { // If we don't have a parent state machine then bail if (_zombieStateMachine == null) { return; } // We are not interested in exit events so only step in and process if its an // enter or stay. if (eventType != AITriggerEventType.Exit) { // What is the type of the current visual threat we have stored AITargetType curType = _zombieStateMachine.VisualThreat.type; // Is the collider that has entered our sensor a player if (other.CompareTag("Player")) { // Get distance from the sensor origin to the collider float distance = Vector3.Distance(_zombieStateMachine.sensorPosition, other.transform.position); // If the currently stored threat is not a player or if this player is closer than a player // previously stored as the visual threat...this could be more important if (curType != AITargetType.Visual_Player || (curType == AITargetType.Visual_Player && distance < _zombieStateMachine.VisualThreat.distance)) { // Is the collider within our view cone and do we have line or sight RaycastHit hitInfo; if (ColliderIsVisible(other, out hitInfo, _playerLayerMask)) { // Yep...it's close and in our FOV and we have line of sight so store as the current most dangerous threat _zombieStateMachine.VisualThreat.Set(AITargetType.Visual_Player, other, other.transform.position, distance); } } } else if (other.CompareTag("Flash Light") && curType != AITargetType.Visual_Player) { BoxCollider flashLightTrigger = (BoxCollider)other; float distanceToThreat = Vector3.Distance(_zombieStateMachine.sensorPosition, flashLightTrigger.transform.position); float zSize = flashLightTrigger.size.z * flashLightTrigger.transform.lossyScale.z; float aggrFactor = distanceToThreat / zSize; if (aggrFactor <= _zombieStateMachine.sight && aggrFactor <= _zombieStateMachine.intelligence) { _zombieStateMachine.VisualThreat.Set(AITargetType.Visual_Light, other, other.transform.position, distanceToThreat); } } else if (other.CompareTag("AI Sound Emitter")) { SphereCollider soundTrigger = (SphereCollider)other; if (soundTrigger == null) { return; } // Get the position of the Agent Sensor Vector3 agentSensorPosition = _zombieStateMachine.sensorPosition; Vector3 soundPos; float soundRadius; AIState.ConvertSphereColliderToWorldSpace(soundTrigger, out soundPos, out soundRadius); // How far inside the sound's radius are we float distanceToThreat = (soundPos - agentSensorPosition).magnitude; // Calculate a distance factor such that it is 1.0 when at sound radius 0 when at center float distanceFactor = (distanceToThreat / soundRadius); // Bias the factor based on hearing ability of Agent. distanceFactor += distanceFactor * (1.0f - _zombieStateMachine.hearing); // Too far away if (distanceFactor > 1.0f) { return; } // if We can hear it and is it closer then what we previously have stored if (distanceToThreat < _zombieStateMachine.AudioThreat.distance) { // Most dangerous Audio Threat so far _zombieStateMachine.AudioThreat.Set(AITargetType.Audio, other, soundPos, distanceToThreat); } } else // Register the closest visual threat if (other.CompareTag("AI Food") && curType != AITargetType.Visual_Player && curType != AITargetType.Visual_Light && _zombieStateMachine.satisfaction <= 0.9f && _zombieStateMachine.AudioThreat.type == AITargetType.None) { // How far away is the threat from us float distanceToThreat = Vector3.Distance(other.transform.position, _zombieStateMachine.sensorPosition); // Is this smaller then anything we have previous stored if (distanceToThreat < _zombieStateMachine.VisualThreat.distance) { // If so then check that it is in our FOV and it is within the range of this // AIs sight RaycastHit hitInfo; if (ColliderIsVisible(other, out hitInfo, _visualLayerMask)) { // Yep this is our most appealing target so far _zombieStateMachine.VisualThreat.Set(AITargetType.Visual_Food, other, other.transform.position, distanceToThreat); } } } } }
/// <summary> /// Called by the parent state machine threats enter/stay/exit the zombie`s sensor trigger. /// Examines the threat and stores it in the parent machine Visual or Audio threat members, /// if found to be the a higher priority threat /// </summary> public override void OnTriggerEvent(AITriggerEventType eventType, Collider other) { if (zombieStateMachine == null) { return; } if (eventType != AITriggerEventType.Exit) { AITargetType currentType = zombieStateMachine.visualThreat.Type; if (other.CompareTag("Player")) { float distance = Vector3.Distance(zombieStateMachine.SensorPosition, other.transform.position); if (currentType != AITargetType.VisualPlayer || (currentType == AITargetType.VisualPlayer && (distance < zombieStateMachine.visualThreat.Distance))) { if (ColliderIsVisible(other, out RaycastHit hitInfo, playerLayerMask)) { zombieStateMachine.visualThreat.Set(AITargetType.VisualPlayer, other, other.transform.position, distance); } } } else if (other.CompareTag("Flash Light") && currentType != AITargetType.VisualPlayer) { BoxCollider flashLightTrigger = (BoxCollider)other; float distanceToThreat = Vector3.Distance(zombieStateMachine.SensorPosition, flashLightTrigger.transform.position); float zSize = flashLightTrigger.size.z * flashLightTrigger.transform.lossyScale.z; float aggravationFactor = distanceToThreat / zSize; if (aggravationFactor <= zombieStateMachine.Sight && aggravationFactor <= zombieStateMachine.Intelligence) { zombieStateMachine.visualThreat.Set(AITargetType.VisualLight, other, other.transform.position, distanceToThreat); } } else if (other.CompareTag("AI Sound Emitter")) { SphereCollider soundTrigger = (SphereCollider)other; if (soundTrigger == null) { return; } Vector3 agentSensorPosition = zombieStateMachine.SensorPosition; Vector3 soundPos; float soundRadius; ConvertSphereColliderToWorldSpace(soundTrigger, out soundPos, out soundRadius); float distanceToThreat = (soundPos - agentSensorPosition).magnitude; float distanceFactor = distanceToThreat / soundRadius; // Bias the factor based on hearing ability of Agent distanceFactor += distanceToThreat * (1.0f - zombieStateMachine.Hearing); if (distanceFactor > 1.0f) { return; } if (distanceToThreat < zombieStateMachine.audioThreat.Distance) { zombieStateMachine.audioThreat.Set(AITargetType.Audio, other, soundPos, distanceToThreat); } } else if (other.CompareTag("AI Food") && currentType != AITargetType.VisualPlayer && currentType != AITargetType.VisualLight && zombieStateMachine.Satisfaction <= 0.9f && zombieStateMachine.audioThreat.Type == AITargetType.None) { float distanceToThreat = Vector3.Distance(other.transform.position, zombieStateMachine.SensorPosition); if (distanceToThreat < zombieStateMachine.visualThreat.Distance) { RaycastHit hitInfo; if (ColliderIsVisible(other, out hitInfo, visualLayerMask)) { zombieStateMachine.visualThreat.Set(AITargetType.VisualFood, other, other.transform.position, distanceToThreat); } } } } }
public override void OnTriggerEvent(AITriggerEventType eventType, Collider other) { if (_zombieStateMachine == null) { return; } if (eventType != AITriggerEventType.EXIT) { AITargetType curType = _stateMachine.visualThreat.type; if (other.CompareTag("Player")) { float distance = Vector3.Distance(_zombieStateMachine.sensorPosition, other.transform.position); if (curType != AITargetType.VISUAL_PLAYER || (curType == AITargetType.VISUAL_PLAYER && distance < _zombieStateMachine.visualThreat.distance)) { RaycastHit hitInfo; if (ColliderIsVisable(other, out hitInfo, _playerLayerMask)) { //its close and in our FOV _zombieStateMachine.visualThreat.Set(AITargetType.VISUAL_PLAYER, other, other.transform.position, distance); } } } else if (other.CompareTag("Flash Light") && curType != AITargetType.VISUAL_PLAYER) { BoxCollider flashLightTrigger = (BoxCollider)other; float distanceToThreat = Vector3.Distance(_zombieStateMachine.sensorPosition, flashLightTrigger.transform.position); float zSize = flashLightTrigger.size.z * flashLightTrigger.transform.lossyScale.z; float aggrFactor = distanceToThreat / zSize; if (aggrFactor <= _zombieStateMachine.sight && aggrFactor <= _zombieStateMachine.intelligence) { _zombieStateMachine.visualThreat.Set(AITargetType.VISUAL_LIGHT, other, other.transform.position, distanceToThreat); } } else if (other.CompareTag("AI Sound Emitter")) { SphereCollider soundTrigger = (SphereCollider)other; if (soundTrigger == null) { return; } //Get the position of the Agent Sensor Vector3 agentSensorPosition = _zombieStateMachine.sensorPosition; Vector3 soundPos; float soundRadius; AIState.ConvertSphereColliderToWorldSpace(soundTrigger, out soundPos, out soundRadius); //How far inside the sounds radius are we float distanceToThreat = (soundPos - agentSensorPosition).magnitude; //calculates distance factor 1 when at edge 0 at centre float distanceFactor = (distanceToThreat / soundRadius); //bias the factor based on hearing ability of agent distanceFactor += distanceFactor * (1.0f - _zombieStateMachine.hearing); //Too Far away if (distanceFactor > 1.0f) { return; } //TODO issue is here //if we can hear it and it is closer than what we previously have stored if (distanceToThreat < _zombieStateMachine.audioThreat.distance) { //Most dangerous threat so far _zombieStateMachine.audioThreat.Set(AITargetType.AUDIO, other, soundPos, distanceToThreat); } } else if (other.CompareTag("AI Food") && curType != AITargetType.VISUAL_PLAYER && curType != AITargetType.VISUAL_LIGHT && _zombieStateMachine.satisfaction <= 0.9f && _zombieStateMachine.audioThreat.type == AITargetType.NONE) { float distanceToThreat = Vector3.Distance(other.transform.position, _zombieStateMachine.sensorPosition); if (distanceToThreat < _zombieStateMachine.visualThreat.distance) { RaycastHit hitInfo; if (ColliderIsVisable(other, out hitInfo, _visualLayerMask)) { _zombieStateMachine.visualThreat.Set(AITargetType.VISUAL_FOOD, other, other.transform.position, distanceToThreat); } } } } }
//Esamina la minaccia e la salva nella "Parent Machine" se la minaccia visiva o uditiva //che è stata trovata ha una priorità più alta public override void OnTriggerEvent(AITriggerEventType eventType, Collider other) { if (_zombieStateMachine == null) { return; } if (eventType != AITriggerEventType.Exit) { //Prendo il tipo dell'attuale minaccia visiva che ho salvato AITargetType curType = _zombieStateMachine.VisualThreat.type; //Controllo che il collider entrato nel Sensor sia un Player if (other.CompareTag("Player")) { //Calcolo la distanza tra il sensor e il collider float distance = Vector3.Distance(_zombieStateMachine.sensorPosition, other.transform.position); //Se l'ultima minaccia salvata non è un Player o se questo player è più vicino rispetto //al player salvato come minaccia visiva cambio l'ordine di priorità if (curType != AITargetType.Visual_Player || (curType == AITargetType.Visual_Player && distance < _zombieStateMachine.VisualThreat.distance)) { RaycastHit hitInfo; if (ColliderIsVisible(other, out hitInfo, _playerLayerMask)) { _zombieStateMachine.VisualThreat.Set(AITargetType.Visual_Player, other, other.transform.position, distance); } } } else if (other.CompareTag("Flashlight") && curType != AITargetType.Visual_Player) { BoxCollider flashLightTrigger = (BoxCollider)other; float distanceToThreat = Vector3.Distance(_zombieStateMachine.sensorPosition, flashLightTrigger.transform.position); float zSize = flashLightTrigger.size.z * flashLightTrigger.transform.lossyScale.z; float aggrFactor = distanceToThreat / zSize; if (aggrFactor <= _zombieStateMachine.sight && aggrFactor <= _zombieStateMachine.intelligence) { _zombieStateMachine.VisualThreat.Set(AITargetType.Visual_Light, other, other.transform.position, distanceToThreat); } } else if (other.CompareTag("AI Sound Emitter")) { SphereCollider soundTrigger = (SphereCollider)other; if (soundTrigger == null) { return; } //Prendo la posizione dell'Agent Sensor Vector3 agentSensorPosition = _zombieStateMachine.sensorPosition; Vector3 soundPos; float soundRadius; AIState.ConvertSphereColliderToWorldSpace(soundTrigger, out soundPos, out soundRadius); //Calcolo QUANTO siamo nel raggio sonoro float distanceToThreat = (soundPos - agentSensorPosition).magnitude; //Calcolo un fattore distanza che corrisponde a 1 quando è al limite del raggio sonoro , //0 quando è al centro float distanceFactor = (distanceToThreat / soundRadius); //Calcolo il distanceFactor in base alla capacità uditiva dell'Agent distanceFactor += distanceFactor * (1.0f - _zombieStateMachine.hearing); //Controllo se è troppo distante if (distanceFactor > 1.0f) { return; } //Se la sorgente è udibile e più vicina rispetto a l'ultima che è stata salvata if (distanceToThreat < _zombieStateMachine.AudioThreat.distance) { //Setto la sorgente più vicina _zombieStateMachine.AudioThreat.Set(AITargetType.Audio, other, soundPos, distanceToThreat); } } else // Resgistro la minaccia visiva più vicina if (other.CompareTag("AI Food") && curType != AITargetType.Visual_Player && curType != AITargetType.Visual_Light && _zombieStateMachine.satisfaction <= minSatisfaction && _zombieStateMachine.AudioThreat.type == AITargetType.None) { //Calcolo quanto è distante la minaccia float distanteToThreat = Vector3.Distance(other.transform.position, _zombieStateMachine.sensorPosition); //Controllo se la distanza è minore di qualsiasi copsa sia stata salvata in prcedenza if (distanteToThreat < _zombieStateMachine.VisualThreat.distance) { //Se sì controllo che sia interna al FOV e al relativo range del raggio visivo RaycastHit hitInfo; if (ColliderIsVisible(other, out hitInfo, _visualLayerMask)) { //Setto il target più vicino _zombieStateMachine.VisualThreat.Set(AITargetType.Visual_Food, other, other.transform.position, distanteToThreat); } } } } }