Damage on collision with player..

I have been working on a script for my Enemy AI that I want to lock onto the player, chase it and if it collides with it deals damage to the player. as of right now it is locking on and chasing just fine but when it collides it is not dealing damage. Any idea what I did wrong? (also if any advice can be given as far as to put a timer on how often the collision damage can occur that would be great)

UPDATED SCRIPTS

Enemy Parent

var HP = 100;
var runSpeed : float = 5;
var target : Transform; 

private var controller : CharacterController;

var isChasing : boolean;
var seeDistance : float = 20;

	function Awake(){
	
	controller = transform.GetComponent(CharacterController); 

	var playerCount : int = Network.connections.Length +1;
	}	 

	function Update () {
		
	var tempTarget : Transform = GameObject.Find("Player").transform;
		if (!target){
			if (tempTarget){
			target = tempTarget;
			}
		}
		
	if (target) { 	
		var forward : Vector3 = transform.TransformDirection(Vector3.forward);		
		
		transform.LookAt(target);
		transform.rotation.x = 0;
		transform.rotation.z = 0;
		controller.SimpleMove(forward * runSpeed);
		
		var gos : GameObject[];
    gos = GameObject.FindGameObjectsWithTag("Player"); 
    
    var thePlayer:GameObject = gos[0];
	var dist = Vector3.Distance(thePlayer.transform.position,transform.position);
	
	if(dist<seeDistance){
		isChasing= true;
	} else {
		isChasing=false;
	}
		
		}
	}
	
	function ApplyDamage (damage : int) {
		HP -= damage;
		
		if (HP < 1){
			Die();
		}
	}

	function Die(){
		Destroy(gameObject);
		gameObject.Find("Player").SendMessage ("Heal", 10);
	}
	
	@script RequireComponent(CharacterController)

Enemy Child

var damage = 10;
var hitDelay : float = 0.5;
private var nextHitAllowed : float;

function OnTriggerEnter (col : Collider) {
    if(col.gameObject.tag == "Player"){
        if(Time.time > nextHitAllowed){
            SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
            nextHitAllowed = Time.time + hitDelay;
    Debug.Log("Hit Player");
        }
    }
}

Player Script (the one that deals with health and death)

static var health : float = 100;
var maxHealth = 100;
var degen : float = 1;


function Update(){

		ApplyDamage(degen*Time.deltaTime);
		HealthManager.hitPoints = CharacterDamage.health;
	}


function ApplyDamage (amount : float){

	health -= amount;
	if(health < 1)
		Die();
}

function Die(){
	Application.LoadLevel (0);
	}

function Heal(points : float) {
    health = Mathf.Min(100.0, health + points);
	}

Edit Note : Also I noticed that my line CharacterDamage.health += 10; allows for the health value to go over 100 (I assume simply by increasing the number rather then healing? What would I need to do to fix this? I am assuming it is something along the lines of SendMessage(“Heal”, 10.0, SendMessageOptions.DontRequireReceiver); and adding a heal function to the player?

thanks in advance.

Update : After using Debug.Log I have realized that there is no collision being detected and I have no reason why. Any thoughts on this may solve the issue of not taking damage.

Update 2 : I have solved the healing upon enemy death but can still not get the collission to be detected to apply damage.

hi, i know this is an old thread, just wanted a way to communicate with you, and ask 2 things, did you managed to solve your collision problem, and second question is , how would you do to use this script but for a 2d type game, i mean when you use vector3, if the gameobject was facing cam, it will rotate at 90 degrees

2 Answers

2

OnCollisionEnter() is a rigidbody method, not CharacterController. Even if you add a Rigidbody to your CharacterController object, its wonky at best, they're not really designed to be used together and that's more of a hack.

You could:

-Use OnControllerColliderHit() instead of OnCollisionEnter(). OnControllerColliderHit() is similar to OnCollisionEnter() but for CharacterControllers, except it only fires when this CharacterController is performing a Move command (so if the player runs into the enemy and the enemy isn't moving, OnControllerColliderHit() won't fire on the enemy [but it will fire on the player, because he was obviously moving])

-Make your enemies rigidbodies instead of CharacterControllers (and then use translation/force/velocity instead of move commands)

-Parent a GameObject to the enemy with no renderer and a sphere collider set to be a trigger, and have that send damage messages with OnTriggerEnter().

Edit: Oh, and as for the timer, just make sure that Time.time is greater than the time of the last hit + the delay you want between hits:

var hitDelay : float = 0.5;
private var nextHitAllowed : float;

function OnCollisionEnter (col : Collision) {
    if(col.gameObject.tag == "Player"){
        if(Time.time > nextHitAllowed){
            SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
            nextHitAllowed = Time.time + hitDelay;
        }
    }
}

As for the last part of your comment, you'd probably want to parent an empty gameobject with sphere collider to the enemy, not parent the enemy to it. But then you'd have to add an extra script to that child trigger that handles the damage message sending in OnTriggerEnter(); you wouldn't be able to do that from a script on it's parent.

The empty gameobject with the sphere collider trigger should be nothing but the collider set to trigger and a single script on it, that send damage messages to the player in OnTriggerEnter(). Then make that gameobject a child of the object with the enemy mesh model, charactercontroller, movement and health script.

Ok so now I have a Parent item with Char Controller, Move/hp script, Mesh Model, and a Child Object that only has Sphere collider set to be a trigger and a script for OnTriggerEnter. The Two scripts being used are both being updated into the Original post, as well as the script on my player (in case something is smessed up there?) As of right now Debug.Log is still not detecting any form of collision and player is not taking any damage.

Ok update, I went in and removed the rigidbody and capsule collider from my fps controller and now Debug.Log is registering collision but no damage is being applied still.

You want to just upload a unitypackage of your project somewhere and I'll take a look for you? Or at least a package of these particular scripts/objects stripped out and put in a scene?

The way I do it is I have a collision detection on the player and if it collides with an enemy then I call a LooseHealth function. as for looking and and moving too I use the LookAt function in mono-develop and and add a forward force.