private void punch() { // animate punch animator.SetInteger("PunchState", 1); if (has_double_speed) { animator.speed = animation_speed * 2.5f; } else { animator.speed = animation_speed; } // get player forward vector from scale Vector3 forward_vector; if (gameObject.transform.localScale.x < 0.0f) { forward_vector = new Vector3(1.0f, 0.0f, 0.0f); } else { forward_vector = new Vector3(-1.0f, 0.0f, 0.0f); } bool hit = false; foreach (GameObject o in objects_in_range) { // dont apply damage or force to self if (gameObject.Equals(o)) { continue; } if (o == null) { continue; } // only apply forces and damage to objects in front of player Vector3 vec_to_other = (o.transform.position - gameObject.transform.position); // get intersection distance with collider float distance_to_other = Mathf.Infinity; Collider2D collider = o.GetComponent <Collider2D>(); Vector3 closest_point = transform.position; if (collider != null) { closest_point = collider.bounds.ClosestPoint(gameObject.transform.position); distance_to_other = Mathf.Max(Vector3.Distance(closest_point, gameObject.transform.position), 0.0f); if ((Vector3.Dot(vec_to_other.normalized, forward_vector) < 0.0f && distance_to_other > 0.1) || distance_to_other > 1.1f) { // other object is not in front of player continue; } } // deal damage HealthScript health_script = o.GetComponent <HealthScript>(); if (health_script != null) { if (health_script.health <= 0.0f) { continue; } if (has_double_damage) { health_script.takeHealth(punch_damage * 2.0f); } else { health_script.takeHealth(punch_damage); } // stun if enemy if (o.tag.Contains("Enemy")) { EnemyCommander commander = o.GetComponent <EnemyCommander>(); if (commander != null) { commander.stun(); } } } // add force Rigidbody2D rigid_body = o.GetComponent <Rigidbody2D>(); if (rigid_body != null) { // Display punch sprite playPunchEffects(forward_vector); hit = true; Vector3 direction = (forward_vector + new Vector3(0.0f, Random.Range(0.5f, 0.8f), 0.0f)).normalized; if (has_double_damage) { rigid_body.AddForce(direction * punch_force * 2.0f); } else { rigid_body.AddForce(direction * punch_force); } } } if (!hit) { SoundEffectScript.Instance.playSwooshSound(transform.position); } }
void Awake() { EnemyCommanderInstance = this; EDictionary = new Dictionary <string, EnemyInfo>(); GetEnemyInfo(); }