- Home /
problems with OnControllerColliderHit
hey i try to make my enemy kill my, if he collides with my player. so here are the scripts i use for this (the first one is enemy and the second one is a script that only handles everything related to health):
var target : Transform; static var hitPoints : int = 50; var huntRange : float = 10.0; var rotationSpeed : float = 0.1; var moveSpeed : float = 3.0; var runSpeed : float = 5.0; private var speed : float;
 
               function OnControllerColliderHit(hit : ControllerColliderHit) { var Hit = hit.collider; if(hit.gameObject.tag == "Player") { health.Hit = true; } }
 function Start() { target = GameObject.FindWithTag("Player").transform; }
 function Update() { var hit : RaycastHit; var direction : Vector3 = target.position - transform.position;
       if( direction.magnitude < huntRange || Physics.Raycast(transform.position , direction, hit)                            &&hit.collider.gameObject == target.gameObject)
         if(Player.running) speed = runSpeed;
         else speed = moveSpeed;
         transform.rotation = Quaternion.Slerp(transform.rotation,
                                 Quaternion.LookRotation(direction),
                                 rotationSpeed);
         transform.position += transform.forward * speed * Time.deltaTime;
 } 
and now the health script:
static var player_dead : boolean = false; static var Hit : boolean = false; var health : int = 340; var mana : int = 300; var recover : int = 10; var array = [health,mana, recover];
 
                function Update() {
     if(Hit == true && health >=0) {
             health -= enemy.hitPoints;
             if(health <= 0) {
                  health = array [0];
                  player_dead = true;
         }
     }
     else {
         health += recover * Time.deltaTime;
     }
     }
 function OnGUI () {
     GUI.BeginGroup(Rect(Screen.width / 2 - 550, Screen.height / 2 - 430 , 400, 100));
     GUI.Box(Rect(0,0,400,100),"Health");
     GUI.Box(Rect(30,20,health, 20),"");
     GUI.Box(Rect(30,60,mana, 20),"");
     GUI.EndGroup(); 
 }
  
Answer by Apexity · Apr 05, 2013 at 01:22 PM
Hey Juri,
Delete the Hit variable from inside the health script. If the health script is attached to the player then you can replace health.Hit = true; from inside your enemy script with Hit.GetComponent(health).health - //However much damage you want to deal; And, I believe the problem is forming from the naming of your variables. Which are both named Hit. Anywho, This should clean up your script a bit and should eliminate the problem. However, Unity isn't on this computer so I cant test it out right now. Get back to me if it continues to cause a problem. Hope this helped :D 
Your answer
 
 
             Follow this Question
Related Questions
Destroy character controller and instantiate new one 1 Answer
CharacterController "breaks" collision, goes haywire 2 Answers
Character controllers not colliding with each other 1 Answer
Error on checking collision or code desync? 0 Answers
Deform Charater controler Collider acording to stance 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                