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; } } } } }
void TakeInput() { if (m_CurrentSelectedPlayer == null) { return; } if (Input.touchCount > 0) { Touch touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Ended) { /* * RaycastHit hit; * * Ray rr = m_Camera.ScreenPointToRay (touch.position); * * if (Physics.Raycast(rr, out hit) ) * { * NavNode nearestNode = m_NavGrid.GetNearestNode (hit.point.x, hit.point.z); * * if (nearestNode != null) * { * var path = m_NavGrid.GetPath (m_LocalPlayer.m_CurrentNavGridRow, m_LocalPlayer.m_CurrentNavGridColumn, nearestNode.m_RowIndex, nearestNode.m_ColumnIndex); * * m_LocalPlayer.SetPathAndMove (path); * } * } */ } } if (Input.GetMouseButtonDown(0)) { m_TimePressedDown = Time.time; RaycastHit hit; Ray rr = m_Camera.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(rr, out hit)) { Vector3 diff = hit.point - m_CurrentSelectedPlayer.GetComponent <Rigidbody>().transform.position; diff.Normalize(); m_CurrentSelectedPlayer.SetDirection(diff.x, diff.z); } } else if (Input.GetMouseButtonUp(0)) { float timeSincePressed = Time.time - m_TimePressedDown; if (timeSincePressed > kShootHoldMinTime) { if (timeSincePressed > kShootHoldMaxTime) { timeSincePressed = kShootHoldMaxTime; } float percentageForce = timeSincePressed / kShootHoldMaxTime; Vector3 diff = m_Football.GetComponent <Rigidbody>().transform.position - m_CurrentSelectedPlayer.GetComponent <Rigidbody>().transform.position; float magSqrAbs = Mathf.Abs(diff.sqrMagnitude); // jump m_CurrentSelectedPlayer.GetComponent <Rigidbody>().AddForce(new Vector3(0, 600 * percentageForce, 0), ForceMode.Impulse); if (magSqrAbs < 15) { ShootBall(12.0f * percentageForce, m_CurrentSelectedPlayer.GetComponent <Rigidbody>().transform.forward); m_TimeLeftUntilCanSwitchPlayer = 0.0f; } } // this is so we can determine that we are not pressing down m_TimePressedDown = -1.0f; } }