Damage Player when this object collides the CharacterController?

Well lets say this script is attached to a box, that box is a child of my Enemy and has a box collider and a kinematic rigidbody because if not it mess up. Im trying to do that when this object collides with player charactercontroller the Player gets damaged.
Here is what i tryed. Raycasting works but is not what im looking for, I also tryed OnCollisionEnter butdidnt worked.And i dont know if im using OnControllerColliderHit correctly. So can you give me any solution? Please!!

var attack = 30;
var canattack = 1;
var cooldown : double = 2.2;
var distance : double = 0.5;

//function Update() {
//	if(Enemy.GetComponent(EnemyAI).attackEnabled == 0) {
//	var fwd = transform.TransformDirection (Vector3.forward);
//	var hit : RaycastHit;
//	if(canattack == 1) {
//		if (Physics.Raycast (transform.position, fwd, hit, distance)) {
//			if (hit.collider.gameObject.tag == "Player") {
//				hit.collider.gameObject.GetComponent(PlayerHealth).damage += attack;
//				print ("Ouch!That Hurts");
//				canattack = 0;
//				Attack();
//			}
//		}
//	}
//}
//function Attack() {
//	while(true) {
//		if(canattack == 0) {	
//			yield WaitForSeconds(cooldown);
//			canattack = 1;
//		}
//		else {
//			yield;
//		}
//	}
//}

//function OnCollisionEnter(collision : Collision) {
//	print ("Collide!");
//	if (collision.gameObject.tag == "Player") {
//		collision.gameObject.GetComponent(PlayerHealth).damage += attack; 	
//	print ("Ouch!");
//	}
//}

//function OnControllerColliderHit (hit : ControllerColliderHit) {
//	if (hit.collider.tag == "Player") {
//		hit.collider.GetComponent(PlayerHealth).damage += attack;
//		print ("Ouch!");
// 	}
//}

try doing this way script on box OnTriggerEnter(Collider Other){ Player PlayerScript = Other.gameObject.getComponent("Player") as Player; PlayerScript.Health --; } box has to have collider is trigger true something like this sorry if here are typos but I've written it without unity's help and OFC I write in C#

that is for trigger colliders..

@sdgd Can you translate it to java so i can try it?

function OnTriggerEnter(Collider Other){ var PlayerScript = Other.gameObject.getComponent("Player"); PlayerScript.Health --; } that is his in js

No that doesnt work neither

2 Answers

2

function OnCollisionEnter(collision : Collision) {
print (“Collide!”);
if (collision.gameObject.tag == “Player”) {
// collision.gameObject.GetComponent(PlayerHealth).damage += attack;
collision.gameObject.GetComponent(PlayerHealth).health -= attack;
print (“Ouch!”);
}
}

it looks like you were adding to the damage of the player health script

i added a line where it will subtract from the health instead

Sorry but my script is ok cause: curHealth = maxHealth - damage;

Thats what I tried already but doesnt works withcharacter controllers.

it is printing collide right? and the tag on the player is a capitol P and correct spelling and such. maybe print the colliders name so you can check what it is hitting like this: print("Collide! with " + collision.gameObject.name);

I added that but it seems it doesnt collides with my character.

If i change the box to non-kinematic it works ok but after firsthit it goes flying away

There is no solution for this?

be patient, dont post a new 'answer'. if you want to bump, post a comment, but that is after no activity for several days. remember, we all have lives too

Yes I Know,Im Looking other questions too but none answer my questions.