private Vector3 followerPositionAt(FormationType formation, int index) { switch (formation) { case FormationType.Square: { var right = Vector3.Cross(Direction, Vector3.up); Vector3 position; switch (index) { case 0: position = transform.position - (right + Direction).normalized * Square.Distance; break; case 1: position = transform.position - (-right + Direction).normalized * Square.Distance; break; default: position = transform.position - Direction * Square.Distance * index; break; } var ideal = position; AIUtil.GetClosestStandablePosition(ref position); var closest = Vector3.zero; var hasClosest = false; var minDist = 0f; for (int i = 0; i < _followers.Count; i++) { var dist = Vector3.Distance(_followers[i].transform.position, ideal); if (_followers[i]._obstacle != null && _followers[i]._obstacle.enabled && dist <= _followers[i]._obstacle.radius + float.Epsilon) { dist = 0; } if (!hasClosest || dist < minDist) { closest = _followers[i].transform.position; minDist = dist; hasClosest = true; } } if (hasClosest && Vector3.Distance(position, ideal) > minDist) { position = closest; } return(position); } case FormationType.Line: return(_line[index]); default: Debug.Assert(false); return(Vector3.zero); } }
private void runTo(Vector3 position) { if (_isKeepingCloseTo && Vector3.Distance(position, _keepCloseTo.Position) > _keepCloseTo.Distance) { position = _keepCloseTo.Position + (position - _keepCloseTo.Position).normalized * _keepCloseTo.Distance; } if (AIUtil.GetClosestStandablePosition(ref position)) { Message("ToRunTo", position); } else { OnPositionUnreachable(position); } }
/// <summary> /// Notified by the brains of a new threat position. /// </summary> public void OnThreatPosition(Vector3 position) { if (!_isAssaulting || !isActiveAndEnabled) { return; } _threatPosition = position; if (Vector3.Distance(position, _targetPosition) > 0.5f) { _targetPosition = position; if (AIUtil.GetClosestStandablePosition(ref position)) { Message("ToRunTo", position); } else { OnPositionUnreachable(position); } } }
private void Update() { if ((Disabler != null && Disabler.activeSelf) || EventSystem.current.IsPointerOverGameObject()) { hideMarker(); hideOutline(ref _targetOutline); hideSphere(); if (Target == null) { hideOutline(ref _performerOutline); } else { showOutline(ref _performerOutline, Target, SelectColor); } return; } var camera = _camera; if (camera == null) { camera = Camera.main; } if (camera == null) { return; } var mouse = Input.mousePosition; mouse.z = camera.nearClipPlane; var near = camera.ScreenToWorldPoint(mouse); mouse.z = camera.farClipPlane; var far = camera.ScreenToWorldPoint(mouse); var hit = Util.GetClosestNonActorHit(near, far, 1); var targetActor = AIUtil.FindClosestActorIncludingDead(hit, 0.7f); var isMouseDown = Input.GetMouseButtonDown(0); if (Target == null) { if (targetActor != null && targetActor.Side == Side && targetActor.IsAlive) { showOutline(ref _performerOutline, targetActor, PickColor); if (isMouseDown) { Target = targetActor; isMouseDown = false; } } else { hideOutline(ref _performerOutline); } } else if (Target != null) { showOutline(ref _performerOutline, Target, SelectColor); } else { hideOutline(ref _performerOutline); } AIActions targetActions = null; if (Target != null) { targetActions = Target.GetComponent <AIActions>(); } if (targetActions) { var target = hit; AIUtil.GetClosestStandablePosition(ref target); var isActor = targetActor != null; var isSelf = isActor && targetActor == Target; var isAlly = isActor && targetActor.Side == Target.Side; var isEnemy = isActor && targetActor.Side != Target.Side; var isDead = isActor && !targetActor.IsAlive; var isLocation = Vector3.Distance(target, hit) < 0.5f; AIAction action = null; var isTargetingActor = false; if (_forcedAction != null) { if (_forcedAction.CanTargetGround && isLocation) { action = _forcedAction; } else if ((!isDead || !_forcedAction.ShouldIgnoreDead) && ((isSelf && _forcedAction.CanTargetSelf) || (isAlly && _forcedAction.CanTargetAlly) || (isEnemy && _forcedAction.CanTargetEnemy))) { action = _forcedAction; isTargetingActor = true; } } else { for (int i = 0; i < targetActions.Actions.Length; i++) { var a = targetActions.Actions[i]; if (a.CanTargetGround && isLocation) { action = a; break; } else if ((!isDead || !a.ShouldIgnoreDead) && ((isSelf && a.CanTargetSelf) || (isAlly && a.CanTargetAlly) || (isEnemy && a.CanTargetEnemy))) { action = a; isTargetingActor = true; break; } } } if (action != null) { if (isTargetingActor) { if (isSelf) { hideOutline(ref _performerOutline); } showOutline(ref _targetOutline, targetActor, action.UIColor); hideMarker(); hideSphere(); } else { if (action.UIRadius > 0.001f) { showSphere(target, action.UIColor, action.UIRadius); hideOutline(ref _targetOutline); hideMarker(); } else { showMarker(target, action.UIColor); hideOutline(ref _targetOutline); hideSphere(); } } } if (isMouseDown) { isMouseDown = false; if (action != null) { if (action.CanTargetGround) { targetActions.Execute(action, target); } else { targetActions.Execute(action, targetActor); } if (_forcedAction != null) { _forcedAction = null; } } Target = null; } else if (Input.GetMouseButtonDown(1)) { Target = null; } } else { hideOutline(ref _targetOutline); hideMarker(); hideSphere(); } }