- Home /
Enemy Death and Killing Script
I am working on a side scroller game. you can run around, jump, pick up crates, and stuff like that. What I am having trouble on is making enemies. I want it to be kinda like Braid, or Mario, enemies go back and forth and if you get hit by one you die, but if you jump on it's head it dies. I have the AI enemy script, but I can't make it die, or kill you. PLEASE HELP! I prefer JavaScript, but if you can do it in C# or any other kind that is good too.
Here's my enemy AI script
var pointB : Vector3;
function Start () {
var pointA = transform.position;
while (true) {
yield MoveObject(transform, pointA, pointB, 3.0);
yield MoveObject(transform, pointB, pointA, 3.0);
}
}
function MoveObject (thisTransform : Transform, startPos : Vector3, endPos : Vector3, time : float) {
var i = 0.0;
var rate = 1.0/time;
while (i < 1.0) {
i += Time.deltaTime * rate;
thisTransform.position = Vector3.Lerp(startPos, endPos, i);
yield;
}
}
and here is my character respawn script
static var dead = false;
function Update()
{
//if you die you respawn in same level
if(dead)
{
Application.LoadLevel(Application.loadedLevel);
}
//if you fall out of level you respawn
if(transform.position.y < -25)
{
Application.LoadLevel(Application.loadedLevel);
}
}
function OnCollisionEnter(hit : Collision)
{
//if you hit the enemy you die
if(hit.gameObject.tag == ("Enemy"))
{
dead = true;
}
}
Sorry about the weird script thing, I'm new to unity answers.
To format code, select the code and use the 101/010 button. I've fixed it for you this time.
What does happen? How about throwing some Debug.Log functions in there? Looks like you're checking the collision for the death event correctly.
Answer by llSalvationll · May 28, 2013 at 09:49 PM
All you should need to do is test to see if the player's Y value is greater than the enemy's current Y value + the object's height. If the player's Y is greater than that value(or nearly so) then the enemy dies because the player is above the enemy. If not, the player dies.
Thanks but even if my character hits the enemy, he won't die??
What part of the death process is broken then - is the collision being detected between enemy & player? Throw a debug/print statement in your OnCollisionEnter function to know for sure. If not, make sure the spawned enemies are tagged as "Enemy" and that the objects you want to collide meet the requirements for collision to take place - i.e. rigidbody's and/or colliders, etc. Also make sure that you don't have 'Is Trigger' checked since you're using OnCollisionEnter.
Answer by goat3 · May 30, 2013 at 10:21 PM
Thanks a lot, but right now even if the player hits the enemy he won't die
Answer by oblivion modder · Mar 18, 2015 at 05:37 PM
first attach this script to your enemy's collider and make sure it is called Enemy1.
var fall :boolean;
var stomp : boolean;
function Update () {
if(stomp){
transform.position.z = 4;
transform.localScale.y /= 2.00 ;
fall = true;
stomp = true;
}
if (fall){
transform.position.y -= 0.10;
}
if (transform.position.y<-25){
Destroy(gameObject);
}
}
function OnTriggerEnter (other : Collider) {
if (!stomp){
if (other.tag == "Player"){
Destroy(other.gameObject);
}
}
}
Next create a collider that is called stomp and attach it above of your enemy and make sure it is a sub-object of your enemy. Add this script to it.
function OnTriggerEnter (Player : Collider){ transform.root.gameObject.GetComponent(Enemy1).stomp = true; gameObject.GetComponent(Enemy1).stomp=true; gameObject.GetComponent(Enemy1).fall=true;
}
that should work
Your answer
![](https://koobas.hobune.stream/wayback/20220613103025im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Why is my enemy stopping before it reaches the player? 1 Answer
Instantiate with Prefabs 2 Answers
How do I stop a function from executing? 1 Answer
Trap Door Question 1 Answer