- Home /
collision wont work
im making a game where the player looses energy every time they move and in order to regain energy to have to find wells and collect water.
there are enemies who are trying to catch you. ive written the code up to this point but im very new to unity and i cant get the collision to work.
i want it so that if an enemy hits you, you die (destroy player) and if you hit the well, the water will destroy but your energy will be replenished.
i have objects with tags : Player. enemy, water and well
please help im desperate! the code is in C#:
using UnityEngine;
using System.Collections;
public class player_health : MonoBehaviour {
public int maxHealth = 100; // maximum health
public int curHealth = 100; // current health
public float healthBarLength;
object Player = (GameObject) GameObject.FindWithTag("player");
object Enemy = (GameObject) GameObject.FindWithTag("enemy");
// Use this for initialization
void start() {
healthBarLength = Screen.width / 2;
}
// Update is called once per frame
void Update () {
addjustCurrentHealth(0);
gameOver();
}
void OnGUI() {
GUI.Box (new Rect(10, 10, Screen.width / 2 / (maxHealth / curHealth), 20),curHealth + "/" + maxHealth) ;
if(curHealth < 2){
GUI.Label(new Rect(10,10,Screen.width, Screen.height), "Nearly one in five children die before their fifth birthday in Somalia. This is the world’s highest mortality rate for children. Every child deserves the right to live with access to clean water and without fear of danger. You can make a difference by donating to the Unicef East Africa Emergency Appeal. press enter to restart or press space to visit the website");
if(Input.GetKey(KeyCode.Space))
{
Application.OpenURL("http://www.unicef.org.au/Donate/One-off-Donation/east-africa-drought-emergency-appeal-famine.aspx");
}
}}
public void addjustCurrentHealth(int adj) {
curHealth += adj;
if(curHealth < 1)
//curHealth = 1;
if(curHealth > maxHealth)
curHealth = maxHealth;
if(Input.GetKey (KeyCode.LeftArrow))
curHealth = curHealth - 1 ;
if(Input.GetKey (KeyCode.RightArrow))
curHealth = curHealth -1;
if(Input.GetKey (KeyCode.UpArrow))
curHealth = curHealth - 1;
if(Input.GetKey (KeyCode.DownArrow))
curHealth = curHealth - 1;
}
public void gameOver() {
if (Input.GetKeyDown (KeyCode.Return)) {
Application.LoadLevel (0);
}
}
void onTriggerEnter(BoxCollider other){
if(other.tag == "enemy"){
curHealth -= 50;
Destroy(GameObject.FindWithTag("Player"));
}
if(other.tag == "well"){
Destroy(GameObject.FindWithTag("water"));
}
}
}
I kinda squeeze your code a little, for later posts try to only provide relevant parts, only the collision part is useful from the 50 lines you posted here.
Answer by fafase · May 03, 2012 at 05:27 AM
So you want the well to give you health and the enemy to kill you:
void OnTriggerEnter(Collider other){ // Here you were using the wrong class, Collider is needed, also check the Cap letters
if(other.tag == "enemy"){
curHealth -= 50; // this line not so useful since you destroy the player right after
Destroy(gameObject);
}
if(other.tag == "well"){
curHealth += 50;
Destroy(other.gameObject);
}
}
Try and say if any luck, that should though.