bool IsInConeOfSight(Transform target, float visionAngle) { Vector3 directiontotarget = target.position - AIcontext.Get <Transform>("self").position; // we use the gun instead of player's transform in case of slope // in this case the player might look at target perfectly but the elevation // will make him consider it is not seing it properly float seingvalue = Vector3.Dot( directiontotarget.normalized, AIcontext.Get <Gun>("Gun").mussle.forward); if (AIcontext.Get <bool>("DebugMussle")) { Debug.Log(seingvalue); } if (seingvalue - visionAngle > 0) { return(true); } return(false); }
bool CanBeSeen(Transform enemy) { Ray ray = new Ray( AIcontext.Get <Transform>("self").position, enemy.position - AIcontext.Get <Transform>("self").position); RaycastHit hit = new RaycastHit(); if (Physics.Raycast(ray, out hit)) { if (hit.collider.tag == "Red" || hit.collider.tag == "Blue") { if (hit.collider.name == enemy.name || hit.collider.tag == enemy.tag) { return(true); } else { return(false); } } } if (AIcontext.Get <Transform>("self").name == _name) { Debug.Log("CanBeSeen"); } return(false); }
protected Vector3 GetRandomTargetCloseToPc(PCBehavior pc) { if (pc == null) { Debug.Log(AIcontext.Get <Team>("SelfTeam")); } Vector2 randV2 = UnityEngine.Random.insideUnitCircle * pc.PCRange; // just another security check if (AIcontext.Get <NavMeshAgent>("agent").isOnNavMesh) { AIcontext.Set <PCBehavior>("PcTarget", pc); if (pc == null) { return(AIcontext.Get <Transform>("self").position + new Vector3(randV2.x, 1f, randV2.y)); } else { // We set target to some place around the pc (but still inside) return(pc.transform.position + new Vector3(randV2.x, 1f, randV2.y)); } } return(pc.trans.position); }
public override BTState Run() { if (AIcontext.Get <Gun>("Gun").canShoot) { AIcontext.Get <Gun>("Gun").Shoot(); } return(BTState.Success); }
public override BTState Run() { return(EvaluateTargets(Spawner.Instance.getEnemiesInRange( AIcontext.Get <Team>("SelfTeam"), AIcontext.Get <Transform>("self").position, AIcontext.Get <float>("VisionDistance"))) == true ? BTState.Success : BTState.Failure); }
public override BTState Run() { if (AIcontext.Get <bool>("DebugCanSee")) { CanBeSeen(AIcontext.Get <Transform>("Target")); } return(CanBeSeen(AIcontext.Get <Transform>("Target")) ? BTState.Success : BTState.Failure); }
public override BTState Run() { AIcontext.Set <Vector3>( RandomPositionName, GetRandomTargetCloseToPc( PCManager.Instance.GetFirstNeutralEnemyPC( AIcontext.Get <Transform>("self").position, AIcontext.Get <Team>("SelfTeam"), AIcontext.Get <bool>("ShallDebug")))); return(BTState.Success); }
public override BTState Run() { Transform target = AIcontext.Get <Transform>("Target"); if (AIcontext.Get <bool>("ShallDebug")) { Debug.Log(target == null ? "HasTarget -> no" : ("HasTarget -> yes " + target.name)); } // if (target == null || //!Spawner.Instance.isAlive(target) || //!CheckDistance(target) || //!IsInConeOfSight(target) || //!CanBeSeen(target)) // { // AIcontext.Set<Transform>("Target", null); // return BTState.Failure; // } if (target == null) { if (AIcontext.Get <Transform>("self").name == _name) { Debug.Log("HasTarget == null"); } AIcontext.Set <Transform>("Target", null); return(BTState.Failure); } if (!Spawner.Instance.isAlive(target)) { AIcontext.Set <Transform>("Target", null); return(BTState.Failure); } if (!CheckDistance(target)) { AIcontext.Set <Transform>("Target", null); return(BTState.Failure); } if (!IsInConeOfSight(target)) { AIcontext.Set <Transform>("Target", null); return(BTState.Failure); } if (!CanBeSeen(target)) { AIcontext.Set <Transform>("Target", null); return(BTState.Failure); } return(BTState.Success); }
bool CheckDistance(Transform target) { if (Vector3.Distance( AIcontext.Get <Transform>("self").position, target.position) > AIcontext.Get <float>("VisionDistance")) { return(false); } if (AIcontext.Get <Transform>("self").name == _name) { Debug.Log("CheckDistance"); } return(true); }
public override BTState Run() { var agent = AIcontext.Get <NavMeshAgent>("agent"); if (agent == null) { Debug.Log("Couldn't find any agent"); return(BTState.Failure); } agent.isStopped = false; return(BTState.Success); }
public override BTState Run() { // If something wasn't set if (AIcontext.Get <Transform>(TargetName) == null || AIcontext.Get <float>(ConeOfSight) == 0f) { return(BTState.Failure); } return(IsInConeOfSight( AIcontext.Get <Transform>(TargetName), AIcontext.Get <float>(ConeOfSight)) ? BTState.Success : BTState.Failure); }
bool IsInConeOfSight(Transform enemy) { Vector3 directiontotarget = enemy.position - AIcontext.Get <Transform>("self").position; float seingvalue = Vector3.Dot( directiontotarget.normalized, AIcontext.Get <Transform>("self").forward); if (seingvalue - AIcontext.Get <float>("VisionAngle") > 0) { return(true); } return(false); }
Transform GetClosest(List <Transform> enemies) { Vector3 pos = AIcontext.Get <Transform>("self").position; int index = 0; float dist = float.MaxValue; float tmpDist = 0f; for (int i = 0; i < enemies.Count; i++) { tmpDist = Vector3.Distance(pos, enemies[i].position); if (tmpDist < dist) { dist = tmpDist; index = i; } } return(enemies[index]); }
bool EvaluateTargets(List <Transform> enemies) { if (AIcontext.Get <Transform>("self").name == _name) { Debug.Log((enemies == null || enemies.Count == 0) ? "SelectTarget -> null or 0 enemies found" : ("SelectTarget -> found : " + enemies.Count + " enemies")); } EvalueateInConeOfSight(ref enemies); if (AIcontext.Get <Transform>("self").name == _name) { Debug.Log(enemies.Count == 0 ? "EvalueateInConeOfSight -> no enemies were in cone of sight" : ("EvalueateInConeOfSight -> still : " + enemies.Count + " enemies remains")); } EvaluateSeables(ref enemies); if (AIcontext.Get <Transform>("self").name == _name) { Debug.Log((enemies == null || enemies.Count == 0) ? "EvaluateSeables -> null or 0 enemies were seable" : ("EvaluateSeables -> still : " + enemies.Count + " enemies were seeing.")); } if (enemies.Count > 0) { AIcontext.Set <Transform>( "Target", GetClosest(enemies)); if (AIcontext.Get <Transform>("self").name == _name) { Debug.Log("EvaluateSeables -> Choose : " + AIcontext.Get <Transform>("Target").name); } return(true); } return(false); }
public override BTState Run() { if (AIcontext.Get <Transform>("self").name == _name) { Debug.Log(AIcontext.Get <Transform>(Target).name); } Transform target = AIcontext.Get <Transform>(Target); Transform trans = AIcontext.Get <Transform>("self"); if (target == null || trans == null) { return(BTState.Failure); } var targetPoint = target.position; var targetRotation = Quaternion.LookRotation(targetPoint - trans.position, Vector3.up); trans.rotation = Quaternion.Slerp(trans.rotation, targetRotation, Time.deltaTime); return(BTState.Success); }