void Update() { // Doing setup in Update because scene not fully created in Start() if (!m_IsSetup) { DoSetup(); m_IsSetup = true; return; } if (m_IsInGoalScoredCooldown) { m_timeUntilScoreCooldownFinished -= Time.deltaTime; if (m_timeUntilScoreCooldownFinished <= 0.0f) { m_IsInGoalScoredCooldown = false; m_Football.GetComponent <Rigidbody> ().transform.position = m_Football.m_InitialPosition; } } if (mFriendlyTeam.Count == 0 || mOppositionTeam.Count == 0) { // intitial build RebuildTeams(); m_CurrentSelectedPlayer = GetClosestFriendlyPlayerToBall(); if (m_CurrentSelectedPlayer) { m_CurrentSelectedPlayer.SetPlayerControlled(true); } m_CurrentOppositionPlayer = GetClosestOppositionPlayerToBall(); if (m_CurrentOppositionPlayer) { m_CurrentOppositionPlayer.SetIsAIControlled(true); } } TakeInput(); UpdateCamera(); ApplyBallControlAid(); UpdateCurrentPlayer(); UpdateCurrentOppositionPlayer(); UpdateAIPositions(); }
void UpdateCurrentOppositionPlayer() { if (m_TimeLeftUntilCanSwitchAI > 0.0f) { m_TimeLeftUntilCanSwitchAI -= Time.deltaTime; } if (!m_CurrentOppositionPlayer) { m_CurrentOppositionPlayer = GetClosestOppositionPlayerToBall(); if (m_CurrentOppositionPlayer) { m_CurrentOppositionPlayer.SetIsAIControlled(true); } } else { if (m_TimeLeftUntilCanSwitchAI > 0.0f) { return; } // if the current player is too far away from the ball then select a new player Vector3 diff = m_Football.GetComponent <Rigidbody>().transform.position - m_CurrentOppositionPlayer.GetComponent <Rigidbody>().transform.position; float magSqrAbs = Mathf.Abs(diff.sqrMagnitude); diff.Normalize(); m_CurrentOppositionPlayer.SetDirection(diff.x, diff.z); if (magSqrAbs > 100) { // pick new player float smallestDistance = -1.0f; foreach (FootballPlayer p in mOppositionTeam) { if (p == m_CurrentOppositionPlayer) { continue; } Vector3 diffP = m_Football.GetComponent <Rigidbody>().transform.position - p.GetComponent <Rigidbody>().transform.position; float magSqrAbsP = Mathf.Abs(diffP.sqrMagnitude); if (smallestDistance == -1 || (magSqrAbsP < smallestDistance && (magSqrAbs - magSqrAbsP) > 100)) { Vector3 velocityBeforeSwitch = m_CurrentOppositionPlayer.GetComponent <Rigidbody> ().velocity; m_CurrentOppositionPlayer.SetIsAIControlled(false); smallestDistance = magSqrAbsP; m_CurrentOppositionPlayer = p; m_CurrentOppositionPlayer.SetIsAIControlled(true); m_CurrentOppositionPlayer.GetComponent <Rigidbody> ().velocity = velocityBeforeSwitch; m_TimeLeftUntilCanSwitchAI = 3.0f; } } } } }