public void ReduceHealth(int damage) { if (damage <= hitPoints) { hitPoints -= damage; } else { canAttack = false; hitPoints = 0; ShowMessagesScript script = GameObject.Find("GUI").GetComponentInChildren <ShowMessagesScript>(); string message = "You have killed " + transform.name + "."; script.SetText(message); if (GetComponentInChildren <Animation>().isPlaying) { GetComponentInChildren <Animation>().Stop(); } GetComponentInChildren <Animation>().Play("die"); GameManager.Get().StartCoroutine(Die(GetComponent <Animation>().GetClip("die").length + 2)); } }
public void attack() { float facingDirection = characterController.transform.eulerAngles.y; Vector3 direction = GetDirection(facingDirection, 1.0f); RaycastHit hit; if (Physics.Raycast(characterController.transform.position, direction, out hit, Mathf.Max(moveSpeedX, moveSpeedZ))) { if (hit.transform.tag.Equals("enemy")) { int damage = Random.Range(minDamage, maxDamage); ShowMessagesScript script = GameObject.Find("GUI").GetComponentInChildren <ShowMessagesScript>(); string message = "You hit " + hit.transform.name + " for " + damage.ToString() + " damage."; script.SetText(message); EnemyScript enemyScript = hit.transform.GetComponent <EnemyScript>(); enemyScript.ReduceHealth(damage); ParticleSystem bloodEffect = GetComponentInChildren <ParticleSystem>(); if (!bloodEffect.isPlaying) { bloodEffect.Play(); } } } }
// Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.E)) { if (canSave) { ShowMessagesScript script = GameObject.Find("GUI").GetComponentInChildren <ShowMessagesScript>(); string message = "Saving..."; script.SetText(message); GameData.Instance.locationX = transform.position.x; GameData.Instance.locationZ = transform.position.z; GameData.SaveData(); } else { ShowMessagesScript script = GameObject.Find("GUI").GetComponentInChildren <ShowMessagesScript>(); string message = "Cannot save unless you're next to a checkpoint"; script.SetText(message); } } if (Input.GetKeyDown(KeyCode.L)) { if (canTeleport) { LoadSceneAsync(2); } else { ShowMessagesScript script = GameObject.Find("GUI").GetComponentInChildren <ShowMessagesScript>(); string message = "Cannot load scene unless you're next to the exit door"; script.SetText(message); } } }
private IEnumerator AttackPlayer() { yield return(new WaitForSeconds(3)); while (canAttack) { PlayerResources.ReduceHealth(damage); ShowMessagesScript script = GameObject.Find("GUI").GetComponentInChildren <ShowMessagesScript>(); string message = transform.name + " has hit you for " + damage.ToString() + " damage."; script.SetText(message); if (PlayerResources.hitPoints == 0) { canAttack = false; SceneManager.LoadScene("MenuScene"); } yield return(new WaitForSeconds(3)); } }
private IEnumerator Die(float delay) { yield return(new WaitForSeconds(delay)); ShowMessagesScript script = GameObject.Find("GUI").GetComponentInChildren <ShowMessagesScript>(); string message = "You have received " + reward + " coins."; script.SetText(message); PlayerResources.CollectCoins(reward); GetComponentInChildren <SkinnedMeshRenderer>().enabled = false; foreach (BoxCollider bc in GetComponentsInChildren <BoxCollider>()) { bc.enabled = false; } GetComponentInChildren <Animation>().enabled = false; Destroy(gameObject); }
private void OnTriggerExit(Collider other) { if (other.transform.tag.Equals("enemy")) { GameManager.canAttack = false; } if (other.transform.tag.Equals("checkpoint")) { canSave = false; ShowMessagesScript script = GameObject.Find("GUI").GetComponentInChildren <ShowMessagesScript>(); string message = "Checkpoint range left"; script.SetText(message); } if (other.transform.tag.Equals("exitDoor")) { canTeleport = false; ShowMessagesScript script = GameObject.Find("GUI").GetComponentInChildren <ShowMessagesScript>(); string message = "Exit door range left"; script.SetText(message); } }
private void OnTriggerEnter(Collider other) { if (other.transform.tag.Equals("enemy")) { GameManager.canAttack = true; } if (other.transform.tag.Equals("checkpoint")) { canSave = true; ShowMessagesScript script = GameObject.Find("GUI").GetComponentInChildren <ShowMessagesScript>(); string message = "Press E to save the game"; script.SetText(message); } if (other.transform.tag.Equals("exitDoor")) { canTeleport = true; ShowMessagesScript script = GameObject.Find("GUI").GetComponentInChildren <ShowMessagesScript>(); string message = "Press L to load the next scene"; script.SetText(message); } }