- Home /
Damaging Enemies
Hi, I'm trying to make an enemy in my game, I got the health losing part right, but I can't seem to get the enemy to be killed. I made a single body enemy that is a trigger so I can walk right through it, but it's also that so it can hurt me and stop moving.
var timer = 20;
var forward = false;
var backward = false;
var stop = false;
var timer2 = 0;
var stop2 = false;
private var inputMoveDirection : Vector2 = Vector2.zero;
public static var moveDirection : Vector2 = Vector2.zero;
//by the way, make sure to only use functions when they work
function OnTriggerEnter(){
stop = true;
}
function OnTriggerExit(){
stop2 = true;
}
function Update () {
var controller : CharacterController = GetComponent(CharacterController);
//components needed
moveDirection = transform.TransformDirection(moveDirection);
//Movement
if(stop == false){
if (timer == 20){
forward = true;
backward = false;
}
if (timer == -20){
forward = false;
backward = true;
}
if(forward == true){
moveDirection = Vector2(10, 0);
timer--;
}
if(backward == true){
moveDirection = Vector2(-10, 0);
timer++;
}
}
if(stop == true){
moveDirection = Vector2(0, 0);
}
if (stop2 == true){
timer2++;
if(timer2 == 40){
stop = false;
stop2 = false;
timer2 = 0;
}
}
//force movement into seconds instead of frames
controller.Move(moveDirection * Time.deltaTime);
}
function can_die() {
Destroy(gameObject, 0.0);
}
The CharacterController is off in the "I'm not in the game" part. I have tried to make a script to conquer this beast of a script:
var damage = 2;
var iamthedamage : boolean = false;
function OnTriggerEnter() {
if (Move9.attackready == true){
SendMessage("can_die", damage, SendMessageOptions.DontRequireReceiver);
iamthedamage = true;
}
else {
iamthedamage = false;
}
}
I'm unsure of what the problem is but help would be appreciated.
Answer by Kith · Jun 16, 2011 at 12:47 PM
I take it that the second part, the one with the variable "iamthedamage", is connected to the enemy. Then the function for OnTriggerEnter should probably look something like this.
function OnTriggerEnter(other : Collider) {
if (Move9.attackready == true){
other.gameObject.SendMessage("can_die", damage, SendMessageOptions.DontRequireReceiver);
iamthedamage = true;
} else {
iamthedamage = false;
}
}
That's assuming I understand the question correctly :-)
Your answer
Follow this Question
Related Questions
my enemy is broken 1 Answer
(Solved) Continuous movement 1 Answer
2D Pathfinding Top Down 0 Answers
Rigidbody Platform Character Movement 0 Answers