I’m building a small 2D level for a level design class but there are some scripts that were not taught to us.
I need help with a basic health script that includes damage and death (restarting level) as well as the object that causes it.
I need a script that hurts/kills a player if they come into contact with it.
If the player touches “fire” he dies/gets hurt but if the player presses a key in front of the fire, he puts it out. I also need a script that ends the level when they touch an object.
I’m hoping to get help with this because I don’t know how to script and I’ve only been working with Unity for about 4-5 weeks and we have mostly touched building the level and not programming it.
If possible, I would also like to be able to make the player pick up an item; a key to open the door at the end and a fire extinguisher to put out the fires.
At the very least, could someone direct me as to where I could find the information to make these script??
I will write you one here and now.
PlayerControler.js
var health : int= *user health*;
var maxhealth : int=*user health*;
function ApplyDmg(dmg : int) {
if (health==0) {AreWeDead();}
}
//private protects it from other scripts calling it
private function AreWeDead() {if(health==0){RestartGame();}}
private function RestartGame() {
//And there we meat creator! kidding, it will reload level
Application.LoadLevel(Application.loadedLevel);}
for dmg blocks there is this:
EnemyControler.js
var dmg=1;
function OnCollisionEnter(contact : Collider){
if( contact.tag == "hero") {contact.gameObject.GetComponent(PlayerControler).ApplyDmg(dmg);
}}
//make sure your main player has a tag "hero" on it.