private void Update() { if (agent.enabled == true) { float distance = 100f; var pickUps = GameObject.FindGameObjectsWithTag("Pick Up"); for (int i = 0; i < pickUps.Length; i++) { if (pickUps[i].transform.position.y > 0) { var pickUp = pickUps[i]; float newDistance = Vector3.Distance(pickUp.transform.position, transform.position); if (newDistance < distance) { destinationPosition = pickUp.transform; distance = newDistance; } } } var playerDistance = Vector3.Distance(player.transform.position, transform.position); if (playerDistance < distance) { if (player.getScore() > getScore()) { destinationPosition = player.transform; } } if (destinationPosition != null) { agent.SetDestination(destinationPosition.position); } } }
// Update is called once per frame void Update() { bool allBelow = true; var pickUpsArray = GameObject.FindGameObjectsWithTag("Pick Up"); foreach (GameObject pickUp in pickUpsArray) { if (pickUp.transform.position.y > 0) { allBelow = false; } } if (allBelow) { RollerAgent player = GameObject.FindObjectOfType <RollerAgent>(); EnemyController enemy = GameObject.FindObjectOfType <EnemyController>(); if (player.getScore() > enemy.getScore()) { gameOverText.text = "You Win!"; } else if (player.getScore() == enemy.getScore()) { gameOverText.text = "It's a Draw!"; } else { gameOverText.text = "You lost..."; } //Time.timeScale = 0; } else { gameOverText.text = ""; } }
void OnCollisionEnter(Collision obj) { if (obj.gameObject.CompareTag("Player")) { player = obj.gameObject.GetComponent <RollerAgent>(); var forceVec = -obj.gameObject.GetComponent <Rigidbody>().velocity.normalized * 250f; if (player.getScore() > getScore()) { obj.gameObject.GetComponent <Rigidbody>().AddForce(-forceVec, ForceMode.Force); enemyScore++; } else { agent.enabled = false; var rigidbody = GetComponent <Rigidbody>(); rigidbody.isKinematic = false; rigidbody.AddForce(forceVec, ForceMode.Force); Invoke("EnableNavMesh", 1F); enemyScore--; } SetEnemyScoreText(); } }