/// <summary> /// Gets the closest enemyAI from EnemyList /// </summary> /// <returns>The closest enemy distance.</returns> /// <param name="searchPos">Search position.</param> public float GetClosestEnemyDistance(Vector3 searchPos) { float distance = 9999f; foreach (var ch in CombatHandler.GET_ENEMIES(_team, true)) { if (Vector3.Distance(ch.transform.position, searchPos) < distance) { distance = Vector3.Distance(ch.transform.position, searchPos); } } return(distance); }
/// <summary> /// Assists position. In other words, /// Find the closest target to be hit & moves to a position to better ensure hit. /// </summary> /// <param name="seconds">Seconds.</param> protected void assistPosition(float seconds) { //how close does an enemy need to be for this to be activated float positionValidDistance = 3f; //move position that is this far from the target float distanceFromTarget = 2f; PlayerCombatHandler pch = _weapon.GetOwner() as PlayerCombatHandler; //find close enemy // if (EnemyAIHandler.EnemyList.Count > 0) { if (CombatHandler.GET_ENEMIES(pch.GetTeam(), true).Count > 0) { Transform ownerTransform = _weapon.GetOwner().transform; Vector3 camForward = Vector3.Scale(CameraController.CC.CombatCamera.transform.forward, new Vector3(1, 0, 1)).normalized; Vector3 searchPos = ownerTransform.position + camForward; //if an enemy is close to valid distance, assist position // if (EnemyAIHandler.GetClosestEnemyDistance (searchPos) < positionValidDistance) { if (pch.GetClosestEnemyDistance(searchPos) < positionValidDistance) { CombatHandler closestEnemy = pch.GetClosestEnemy(searchPos); //direction from target to this character Vector3 direction = ownerTransform.position - closestEnemy.transform.position; direction = new Vector3(direction.x, 0f, direction.z); direction = direction.normalized * distanceFromTarget; //position a certain distance(distanceFromTarget) from the target direction = closestEnemy.transform.position + direction; //move towards that position StartCoroutine(MathHelper.IELerpRBPositionOverTime(ownerTransform.GetComponent <Rigidbody> (), ownerTransform.position, direction, seconds)); // //dev // GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); // cube.transform.position = direction; // Debug.Break (); // Quaternion toRotation = Quaternion.LookRotation (direction); // StartCoroutine (MathHelper.IELerpRotationOverTime (ownerTransform, ownerTransform.rotation, toRotation, seconds)); } } }
/// <summary> /// Assists aim. In other words, /// Find the closest target to be hit & aims towards the target automatically. /// </summary> /// <param name="seconds">How fast does this character aim</param> protected void assistAim(float seconds) { float aimValidDistance = 3f; PlayerCombatHandler pch = _weapon.GetOwner() as PlayerCombatHandler; // _targetFound = false; //find close enemy // if (EnemyAIHandler.EnemyList.Count > 0) { if (CombatHandler.GET_ENEMIES(pch.GetTeam(), true).Count > 0) { Transform ownerTransform = _weapon.GetOwner().transform; Vector3 camForward = Vector3.Scale(CameraController.CC.CombatCamera.transform.forward, new Vector3(1, 0, 1)).normalized; Vector3 searchPos = ownerTransform.position + camForward; //if an enemy is close to valid distance, use aimAssist if (pch == null) { Debug.LogWarning("WARNING : Playercharacter must have PlayerCombatHandler"); } else { if (pch.GetClosestEnemyDistance(searchPos) < aimValidDistance) { CombatHandler closestEnemy = pch.GetClosestEnemy(searchPos); // EnemyAIHandler closeEnemey = EnemyAIHandler.GetClosestEnemy (searchPos); // _targetPos = closeEnemey.transform.position; Vector3 direction = closestEnemy.transform.position - ownerTransform.position; direction = new Vector3(direction.x, 0f, direction.z); Quaternion toRotation = Quaternion.LookRotation(direction); StartCoroutine(MathHelper.IELerpRotationOverTime(ownerTransform, ownerTransform.rotation, toRotation, seconds)); // _targetFound = true; } // else { // print ("Not close enough, target : " + EnemyAIHandler.GetClosestEnemy (searchPos).name + ", distance : " + EnemyAIHandler.GetClosestEnemyDistance (searchPos)); // } } } }
/// <summary> /// Gets the closest enemyAI from EnemyList /// </summary> /// <returns>The closest enemy.</returns> /// <param name="searchPos">Search position.</param> public CombatHandler GetClosestEnemy(Vector3 searchPos) { CombatHandler closestCH = null; foreach (var ch in CombatHandler.GET_ENEMIES(_team, true)) { if (ch != null) { if (closestCH == null) { closestCH = ch; } else if (Vector3.Distance(ch.transform.position, searchPos) < Vector3.Distance(closestCH.transform.position, searchPos)) { closestCH = ch; } } else { Debug.LogWarning("WARNING : this photonview must have combathandler as its component"); } } return(closestCH); }
/// <summary> /// Scan for visible enemy /// </summary> public CharacterStatusHandler Scan() { CharacterStatusHandler foundTarget = null; CharacterStatusHandler tempTarget = null; //direction from eyeposition to target Vector3 dir; // //dev // //performance test between loop order and raycast // float startTime = Time.time; //cache List <CombatHandler> enemies = CombatHandler.GET_ENEMIES(_ch.GetTeam(), true); if (!_checkClosest) { enemies.Shuffle(); } //get all enemies in range foreach (var ch in enemies.Where(x => Vector3.Distance(x.transform.position, this.transform.position) < _rangeOfVision && //Check if target are in range of vision Vector3.Angle(transform.forward, x.transform.position - this.transform.position) < _angleOfVision / 2f //Check if target are in angle of vision(Field of vision) )) { //check visibility with raycast LayerMask layerMask = 1 << LayerHelper.COMMON_BODY; RaycastHit hit; if (Physics.Raycast(_eyePosition.position , ch.transform.position - transform.position //Direction from eye to target , out hit, _rangeOfVision + 1f , layerMask )) { tempTarget = ch.GetComponent <CharacterStatusHandler> (); //get closest if (_checkClosest && (foundTarget == null || Vector3.Distance(foundTarget.transform.position, this.transform.position) > Vector3.Distance(tempTarget.transform.position, this.transform.position))) { foundTarget = tempTarget; } else { if (tempTarget != null) { print("scanned : " + tempTarget.name); } //otherwise, get random Target = tempTarget; return(tempTarget); } } } //dev // if (foundTarget != null) { // print ("scanned : " + foundTarget.name); // } //if nothing found, do nearcheck if (foundTarget == null) { foreach (var ch in enemies.Where(x => Vector3.Distance(x.transform.position, this.transform.position) < _nearRoV //Check if target are in range of vision )) { tempTarget = ch.GetComponent <CharacterStatusHandler> (); //get closest if (_checkClosest && (foundTarget == null || Vector3.Distance(foundTarget.transform.position, this.transform.position) > Vector3.Distance(tempTarget.transform.position, this.transform.position))) { foundTarget = tempTarget; } else { if (tempTarget != null) { print("scanned : " + tempTarget.name); } //otherwise, get random Target = tempTarget; return(tempTarget); } } } Target = tempTarget; return(foundTarget); }
void RPCDisplayRemainingEnemyCount() { // UIController.DC.SetDevText("Enemies remaining : " + ((int)_enemyCountTotal + (int)EnemyAIHandler.EnemyList.Count), 1f); UIController.SINGLETON.SetDevText("Enemies remaining : " + ((int)_enemyCountTotal + (int)CombatHandler.GET_ENEMIES(TEAM.ONE, true).Count), 1f); }