Damage Return issue.

So, Picture my scene! I’m up close FPS style against a wall. I press the attack key. Boom. Nothing happens, where the splash damage should have effected me.
In the interest of disclosure (I apologize in advance, the code is messy. Been wracking my brain for hours. I keep getting the error: “ApplyDamage has no reciever”

var explosionRadius = 5.0;
var explosionPower = 10.0;
var explosionDamage = 100.0;
var explosionTime = 1.0;

function Start () {

	var explosionPosition = transform.position;
	var colliders : Collider[] = Physics.OverlapSphere (explosionPosition, explosionRadius);
	Debug.Log (Collider);
	for (var hit in colliders) {
		if (!hit)
			continue;
		if (hit.rigidbody) {
			hit.rigidbody.AddExplosionForce(explosionPower, explosionPosition, explosionRadius, 3.0);
			var closestPoint = hit.rigidbody.ClosestPointOnBounds(explosionPosition);
			var distance = Vector3.Distance(closestPoint, explosionPosition);
			// The hit points we apply fall decrease with distance from the hit point
    	    var damage = 1.0 - Mathf.Clamp01(distance / explosionRadius);
			damage *= explosionDamage;
			Debug.Log (hit.rigidboy);
			// Tell the rigidbody or any other script attached to the hit object 
			// how much damage is to be applied!
			hit.rigidbody.SendMessage("ApplyDamage", damage);
		}
    }

    // destroy the explosion
	Destroy (gameObject, explosionTime);

Thats on the object, which is spawned by the projectile. I’ve checked to see that it is in fact instantiated. So… assuming it exists and went off right next to my ear :slight_smile:

On myself. Collider with the script:

    var health : float;
var healthmin = 0;
var healthmax = 100;
var healthregen = 1;

var mana : float;
var manamin = 0;
var manamax = 100;
var manaregen = 1;

function ApplyDamage (damage : float) {
health -= damage;
}

function Awake () {
}

function Update () {
// regen scripts
		//health += Time.deltaTime * healthregen;
		//health = Mathf.Clamp(health, 0, healthmax);
		//mana += Time.deltaTime * manaregen;
		//mana = Mathf.Clamp(mana, 0, manamax);
		
}

I suspended the regen for the time being in case the interaction was a problem. Guess not sofar. For the record. I’ve preset health and mana @ 10 each. I was Expecting health to drop to zero. No such luck. I also deleted the portion involving the tossing force. (Note: it was based on the FPS tutorial)

On side note: my debugs haven’t displayed anything useful.
The first responds: UnityEngine.Collider
The Second responds: Null
This makes me think that the collision detection isn’t working proper, but since my grasp of that portion is shaky at best I could only check it against the tutorial sintax, hoping it would work.

Any Ideas?

Your Debug.Log are both wrong: the first is printing Collider, but I suppose it should print colliders, and the second is printing hit.rigidboy instead of hit.rigidbody.

But let’s see the main problem: the explosion only affects rigidbodies - does your character have a rigidbody? If you’re using the CharacterController, probably it hasn’t. You can add a rigidbody to a CharacterController, but it will bring some weird reactions to your character. To solve this, you can freeze all axes under Constraints and uncheck Use Gravity - or just try setting Is Kinematic (I didn’t test the last suggestion with OverlapSphere yet).

EDITED: You can add an else clause to the if to handle colliders without rigidbodies:

    if (hit.rigidbody) {
       ... // keep the code for rigidbodies
    }
    else { // for other colliders, calculate damage and send the message
        var distance = Vector3.Distance(hit.transform.position, explosionPosition);
        var damage = 1.0 - Mathf.Clamp01(distance / explosionRadius);
        damage *= explosionDamage;
        // use DontRequireReceiver to avoid boring messages
        hit.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
    }