void Update() { inDanger = enmDect.enemyDetect; if (closest_enemyPoint != null) { AIControl_Blue aiBlue = closest_enemyPoint.GetComponent <AIControl_Blue>(); targetEnemyInPrison = aiBlue.inPrison; } //if (closest_teammatePoint != null) //{ // AIControl aiTeammate = closest_teammatePoint.GetComponent<AIControl>(); // targetTeammateInPrison = aiTeammate.inPrison; //} //if (hasBall) //{ //if (getHit) //{ // if (rb != null) // { // Destroy(rb); // } // } //} }
GameObject findTargetEnemy(GameObject[] enemies) { GameObject closest = null; //you will return this as the person you find. float distance = Mathf.Infinity; Vector3 position = transform.position; foreach (GameObject enemy in enemies) //go through all players in map { var diff = (enemy.transform.position - position); var curDistance = diff.sqrMagnitude; if (curDistance < distance) //is this player closer than the last one? { //AIControl_Blue aiBlueCurrent = enemy.GetComponent<AIControl_Blue>(); aiBlueCurrent = enemy.GetComponent <AIControl_Blue>(); //bool currentTargetEnemyInPrison = aiBlueCurrent.inPrison; currentTargetEnemyInPrison = aiBlueCurrent.goPrison; //bool currentTargetEnemyGoPrison = aiBlueCurrent.goPrison; //if (!currentTargetEnemyInPrison && !currentTargetEnemyGoPrison) if (!currentTargetEnemyInPrison) { closest = enemy; //this is the closest player distance = curDistance; //set the closest distance } } } if (closest != null) { return(closest.gameObject); //this is the closest player } else { if (winNotSet) { winNotSet = false; winText.text = "Red Win !"; gameEndSource.Play(); //return null; } return(null); } }
GameObject findTeammate(GameObject[] teammates) { GameObject closest = null; //you will return this as the person you find. float distance = Mathf.Infinity; Vector3 position = transform.position; foreach (GameObject teammate in teammates) //go through all players in map { var diff = (teammate.transform.position - position); var curDistance = diff.sqrMagnitude; if (curDistance < distance) //is this player closer than the last one? { //AIControl_Blue aiBlueCurrent = enemy.GetComponent<AIControl_Blue>(); teammateCurrent = teammate.GetComponent <AIControl_Blue>(); //bool currentTargetEnemyInPrison = aiBlueCurrent.inPrison; //currentTargetEnemyInPrison = aiBlueCurrent.goPrison; currentTeammateInPrison = teammateCurrent.goPrison; //bool currentTargetEnemyGoPrison = aiBlueCurrent.goPrison; //if (!currentTargetEnemyInPrison && !currentTargetEnemyGoPrison) if (currentTeammateInPrison) { closest = teammate; //this is the closest player distance = curDistance; //set the closest distance } } } if (closest != null) { return(closest.gameObject); //this is the closest player } else { return(null); } }
void Shoot() { getHitRed = false; SetStateText(); if (inDanger) { state = AIControl.State.HIDE; } if (getHit) { if (hasBall) { if (rb != null) { Destroy(rb); } } getHit = false; goPrison = true; state = AIControl.State.SIDELANE; } GameObject shootPlace = findCloestLocation(shootPoint); MoveToPosition(shootPlace); if (hasBall) { if (!shootBall) { Component halo = GetComponent("Halo"); halo.GetType().GetProperty("enabled").SetValue(halo, false, null); } if (reachDestination()) { if (rb != null) { Vector3 newballPosition = this.transform.forward + this.transform.forward; //print("pos: " + newballPosition.ToString()); newballPosition += this.transform.position; newballPosition.y = 1.54f; rb.transform.position = newballPosition; Rigidbody redBall_rb = rb.GetComponent <Rigidbody>(); ////redBall_rb.AddForce(transform.right * 10000);//cannon's x axis closest_teammatePoint = findTeammate(teammatePoint); // target - current position, then normailize it. closest_enemyPoint = findTargetEnemy(enemyPoint); //MoveToPosition(sidePoint); if (closest_teammatePoint != null) { //AIControl aiTeammate = closest_teammatePoint.GetComponent<AIControl>(); Vector3 enemyDirect = closest_teammatePoint.transform.position; enemyDirect.y = 1.6f; Vector3 shootDirect = enemyDirect - (rb.transform.position); Vector3 testVector = new Vector3(1, 0, 1); shootDirect = shootDirect.normalized; redBall_rb.AddForce(shootDirect * thrustForce, ForceMode.Impulse); hasBall = false; shootReach = false; shootBall = true; ballThrowSource.Play(); state = AIControl.State.GETBALL; } else if (closest_enemyPoint != null) { AIControl_Blue aiBlue = closest_enemyPoint.GetComponent <AIControl_Blue>(); targetEnemyInPrison = aiBlue.inPrison; Vector3 enemyDirect = closest_enemyPoint.transform.position; enemyDirect.y = 1.6f; Vector3 shootDirect = enemyDirect - (rb.transform.position); // adding randominess float x_random = UnityEngine.Random.Range(-5.0f, 5.0f); shootDirect.x += x_random; Vector3 testVector = new Vector3(1, 0, 1); shootDirect = shootDirect.normalized; redBall_rb.AddForce(shootDirect * thrustForce, ForceMode.Impulse); hasBall = false; shootReach = false; shootBall = true; ballThrowSource.Play(); state = AIControl.State.GETBALL; } } } } if (hasBall && !shootBall) { //print("sylvia is here 1"); if (rb != null) { Vector3 playerPos = navMeshAgent.transform.position; Vector3 ballPos = playerPos + ballHeightOffset; rb.transform.position = ballPos; //print ("sylvia is here 2"); //rb = Instantiate(redBall, ballPos, navMeshAgent.transform.rotation); } } }