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#
– sdgdthat is for trigger colliders..
– KMKxJOEY1@sdgd Can you translate it to java so i can try it?
– Jeremiasfunction OnTriggerEnter(Collider Other){ var PlayerScript = Other.gameObject.getComponent("Player"); PlayerScript.Health --; } that is his in js
– KMKxJOEY1No that doesnt work neither
– Jeremias