void OnTriggerStay(Collider other) { if (other.gameObject.tag == "Player") { player = other.GetComponent("Player_Health") as Player_Health; player.DamagePlayer(10 * Time.deltaTime); } }
void OnTriggerEnter(Collider other) { if (other.gameObject.tag == "Player") { player = other.GetComponent("Player_Health") as Player_Health; player.DamagePlayer(10); } }
private void atkdmg(Collider2D other) { GameObject player = other.gameObject; Debug.Log(player); Player_Health ph = player.GetComponentInParent <Player_Health>(); Debug.Log(ph); //Debug.Log("dmg"); ph.DamagePlayer(dmg); }
void Update() { // If enemy health reaches 0 if (health < 0) { // stops the navigation mesh navMeshAgent.Stop(); // Set the animation state machine parameters anim.SetBool("dead", true); anim.SetBool("move", false); anim.SetBool("attack", false); // destroys the rigid body temp = this.gameObject.GetComponent("Rigidbody") as Rigidbody; Destroy(temp); // destroy enemy box collider temp2 = this.gameObject.GetComponent("BoxCollider") as BoxCollider; Destroy(temp2); } // if Player is close enough else if (navMeshAgent.stoppingDistance >= Vector3.Distance(target.position, transform.position)) { // If enemy is even close enough to attack if (attack_able) { // set the animation state machine parameters attack_able = false; anim.SetBool("move", false); anim.SetBool("attack", true); // Reduce player health playerhealth.DamagePlayer(10f); // Waits 1 sec to atttack again StartCoroutine(RestoreAttack()); // Stops the navigation mesh agent to let the animation plays without movement navMeshAgent.Stop(); } } // If Enemy move forward to Player's location else if (navMeshAgent.stoppingDistance < Vector3.Distance(target.position, transform.position) && Vector3.Distance(target.position, transform.position) <= 6) { // Mesh Navigation Agent moves forward navMeshAgent.Resume(); // Set animation state machine parameters anim.SetBool("move", true); anim.SetBool("attack", false); // Set the target location navMeshAgent.SetDestination(target.position); } // If Player is far away or unreachable else if (Vector3.Distance(target.position, transform.position) > 5) { // Look for tha base point if (basepoint != null) { // The target postions becomes the base point navMeshAgent.SetDestination(basepoint.position); // Move to reach the base point if (navMeshAgent.stoppingDistance >= Vector3.Distance(target.position, basepoint.position)) { anim.SetBool("move", false); } } // Without base point, just stop if player is far away else { navMeshAgent.Stop(); } } }
//Overrides the base function from Power_FireProjectile. Instead of launching a projectile, it casts a ray to deal damage protected override void FireProjectile() { //Finds the ray start point in front of the player Vector3 startLoc = this.transform.position; startLoc += new Vector3(Mathf.Cos(this.transform.eulerAngles.z * Mathf.Deg2Rad) * this.spawnOffset * this.transform.parent.localScale.x, Mathf.Sin(this.transform.eulerAngles.z * Mathf.Deg2Rad) * this.spawnOffset * this.transform.parent.localScale.x, 0); //Adds angle variance for the ray float startAngle = Random.Range(-this.angleVariance, this.angleVariance) + this.transform.eulerAngles.z; //The XY direction to cast the ray Vector2 direction = new Vector2(Mathf.Cos(startAngle * Mathf.Deg2Rad), Mathf.Sin(startAngle * Mathf.Deg2Rad)); //Creates a new 2D ray to be cast in space RaycastHit2D hitResults = Physics2D.Raycast(startLoc, direction, this.beamRange); //If a player is hit, they take damage if (hitResults.rigidbody != null && hitResults.rigidbody.gameObject.GetComponent <Player_Health>() != null) { Player_Health playerHit = hitResults.rigidbody.gameObject.GetComponent <Player_Health>(); playerHit.DamagePlayer(this.damageDealt); } //Finds the end point of the beam Vector3 endLoc = new Vector3(); if (hitResults.rigidbody == null) { endLoc.x = (direction.x * this.beamRange) + startLoc.x; endLoc.y = (direction.y * this.beamRange) + startLoc.y; } else { endLoc.x = hitResults.point.x; endLoc.y = hitResults.point.y; } //Finds the midpoint of the beam so we can spawn a beam prefab Vector3 midpoint = startLoc + endLoc; midpoint = midpoint / 2; //Creates the beam prefab at the midpoint and rotates to the start angle GameObject beamObject = GameObject.Instantiate(this.projectileToShoot, midpoint, Quaternion.Euler(0, 0, startAngle)) as GameObject; //Scales the beam to match the ray length if (hitResults.rigidbody != null) { beamObject.transform.localScale = new Vector3(beamObject.transform.localScale.x * hitResults.distance, beamObject.transform.localScale.y, beamObject.transform.localScale.z); } else { beamObject.transform.localScale = new Vector3(beamObject.transform.localScale.x * this.beamRange, beamObject.transform.localScale.y, beamObject.transform.localScale.z); } }