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();
}