/// <summary> /// Get the damaging object and apply the defined damage to self. /// </summary> /// <param name="other">Collider of the object with which this collided.</param> void OnTriggerEnter(Collider other) { DamagingObject damagingObject = other.gameObject.GetComponent <DamagingObject>(); if (damagingObject != null) { wasDamaged = damagingObject; } }
/// <summary> /// Get the damaging object and apply the defined damage to self. /// </summary> /// <param name="collision">Collision resulting of OnCollisionEnter.</param> void OnCollisionEnter(Collision collision) { DamagingObject damagingObject = collision.gameObject.GetComponent <DamagingObject>(); if (damagingObject != null) { wasDamaged = damagingObject; } }
/// <summary> /// Get the damaging object and apply the defined damage to self. /// </summary> /// <param name="collision">Collision resulting of OnCollisionEnter.</param> void OnCollisionEnter(Collision collision) { DamagingObject damagingObject = collision.gameObject.GetComponent <DamagingObject>(); if (damagingObject != null && !gameObject.GetComponent <Shield>().shieldIsUp) { wasDamaged = damagingObject; } }
void OnTriggerEnter2D(Collider2D col) { DamagingObject DO = col.GetComponent <DamagingObject>(); if (DO != (null) && col.tag != "Enemy Attack" && col.tag != "Player") { this.health -= DO.Loss; } }
//The "AlertLight" Shenanigans are all done by Oliver, this probably isn't as cool and impeccable ("lol impeccable" -Carlo) as Carlos stuff so just so you know I did this lol. //I just want people to be able to tell the ship is damaged. void ObstacleCollision(Collision other) { DamagingObject obj = other.gameObject.GetComponent <DamagingObject>(); if (obj) { dead = submarineStats.TakeDamage(obj.damage); } else { dead = submarineStats.TakeDamage(submarineStats.defaultCollisionDamage); } collisionInvulnDelay = submarineStats.collisionInvulnTime; CollisionEvent.Raise(); int size = other.contacts.Length; foreach (ContactPoint contact in other.contacts) { subRB.AddForceAtPosition(contact.normal * ((submarineStats.collisionRepelForce * subRB.velocity.magnitude) / size), contact.point, ForceMode.Impulse); } }
/* * void OnCollisionEnter2D (Collision2D col) * { * * Debug.Log ("Player collision"); * // If the colliding gameobject is an Enemy... * if(col.gameObject.tag == "Enemy" || col.gameObject.tag == "DamagingObject") * { * // ... and if the time exceeds the time of the last hit plus the time between hits... * if (Time.time > lastHitTime + repeatDamagePeriod) * { * // ... and if the player still has health... * if(health > 0f) * { * // ... take damage and reset the lastHitTime. * TakeDamage(col); * lastHitTime = Time.time; * } * // If the player doesn't have health, do some stuff, let him fall into the river to reload the level. * else * { * // Find all of the colliders on the gameobject and set them all to be triggers. * Collider2D[] cols = GetComponents<Collider2D>(); * foreach(Collider2D c in cols) * { * c.isTrigger = true; * } * * // Move all sprite parts of the player to the front * SpriteRenderer[] spr = GetComponentsInChildren<SpriteRenderer>(); * foreach(SpriteRenderer s in spr) * { * s.sortingLayerName = "GUI"; * } * * // ... disable user Player Control script * GetComponent<PlayerControl>().enabled = false; * * // ... disable the Gun script to stop a dead guy shooting a nonexistant bazooka * GetComponentInChildren<Gun>().enabled = false; * * // ... Trigger the 'Die' animation state * anim.SetTrigger("Die"); * } * } * } * } */ public void TakeDamage(DamagingObject damaging) { // Create a vector that's from the enemy to the player with an upwards boost. Vector3 hurtVector = transform.position - damaging.transform.position + Vector3.up * 5f; // Add a force to the player in the direction of the vector and multiply by the hurtForce. float damageReceived = damaging.DealDamage(); rigidbody2D.AddForce(hurtVector * hurtForce * damageReceived); Debug.Log(ToString() + " taking " + damageReceived + " damage!"); // Reduce the player's health by 10. health -= damageReceived / (damageResistance + 1.0f); // Update what the health bar looks like. UpdateHealthBar(); // Play a random clip of the player getting hurt. if (ouchClips.Length > 0) { int i = Random.Range(0, ouchClips.Length); AudioSource.PlayClipAtPoint(ouchClips[i], transform.position); } }
/// <summary> /// Apply damage then reset wasDamaged variable. /// </summary> /// <param name="damagingObject"></param> private void GetDamaged(DamagingObject damagingObject) { Debug.Log("Contact with health modifier: " + ((damagingObject.Damage > 0) ? "" : "+") + (-damagingObject.Damage)); CurrentHP -= damagingObject.Damage; wasDamaged = null; }
/// <summary> /// Apply damage then reset wasDamaged variable. /// </summary> /// <param name="damagingObject"></param> private void GetDamaged(DamagingObject damagingObject) { Debug.Log("Getting damaged!"); CurrentHP -= damagingObject.Damage; wasDamaged = null; }