Hey all. I want to have several completely different objects cause damage to the one player’s health. I keep getting close to achieving this but I gave in and decided to get help. haha Thank you.
Can you provide the code you already have written? Is everything a projectile or is some of it a trap like lava? You will be using OnCollisionEnter and OnTriggerEnter or variants of both to make it all work. When an object touches your players collision box, you can have it send a message using those methods above, just look in the documentation. Other than that I would need specifics to be able to help you. I am not in your head!
Sorry about the vagueness. Here’s my code so far for the two objects and the health. One is a bullet or projectile. The other will be a spinning blade of sorts. Eventually there will be enemies and other projectiles, environmental hazards and what not.
Health
static var health : int = 100;
function Update ()
{
if (health <= 0)
{
print(“Dead”);
}
}
Blades (right now it’s a cube for testing purposes)
var damageBox : int = 10;
function OnCollisionEnter(other : Collision)
{
if(other.gameObject.GetComponent(“Health”))
{
if (other.gameObject.tag == “Player”)
{
Health.health -= damageBox * Time.deltaTime;
}
}
}
Bullet
var timeOut : float = 2;
var damageBullet : int = 10;
function Start()
{
Destroy(this.gameObject, timeOut);
}
function OnCollisionEnter(other : Collision)
{
if(other.gameObject.GetComponent(“Health”))
{
Health.health -= damageBullet;
Destroy(this.gameObject);
}
}
I have scripts for the HUD, player movement and the objects firing the bullet. Shall I put those as well?
I am assuming your health is on a script on the player? if it is you can do this
other.gameObject.GetComponent(“HealthScript”).Damage(10);
Where HealthScript is the script you use to keep track of your current health, and Damage() is the function inside the script
On your HealthScript you could make a variable like this
var health : int = 100;
function Update ()
{
if (health <= 0)
{
print(“Dead”);
}
}
function Damage (damageAmount : float)
{
health = health - damageAmount;
}
This way all of your objects damaging your player can access your health script. On your blades script, look up OnCollisionStay. The way you have it setup now, you would need a OnCollisionEnter and OnCollsionExit to start and stop the damage, OnCollisionStay will simplify it.
Hmm. It doesn’t seem to be doing anything now. I’m posting my new code to see what I messed up.
Bullet
var timeOut : float = 2;
var damageBullet : int = 10;
function Start()
{
Destroy(this.gameObject, timeOut);
}
function OnCollisionEnter(other : Collision)
{
other.gameObject.GetComponent(“Health.Damage(damageBullet)”);
Destroy(this.gameObject);
}
Health (on player)
var health : int = 100;
function Update ()
{
if (health <= 0)
{
print(“Dead”);
}
}
function Damage (damageAmount : int)
{
health = health - damageAmount;
}
Blades
var damageBox : int = 10;
function OnCollisionStay(other : Collision)
{
other.gameObject.GetComponent(“Health.Damage(damageBox)”);
}
It makes sense but I must be doing something wrong obviously.
other.gameObject.GetComponent(“Health.Damage(damag eBox)”);
Health.Damage(damag eBox)
Not only is that not a script name, it has a space in a variable name.
It’s only running the GetComponent function and doing nothing with the return.
The syntax is:
GameObject.GetComponent(ScriptName).property
So you would write:
gameObject.GetComponent( Health ).Damage( damageBox );
That space added itself when I pasted the script. I don’t know why. It’s not in my actual script. I also tried the other one too. It’s not giving me any errors. Rather it’s just not doing anything. The blades aren’t harming the player nor are the bullets.
If using Unity 3 it’s likely a good time to get used to using the debugger in MonoDevelop, if not using Unity3, it’s likely a good time to use Unity3.
Wow. That was completely simple. I went back to this issue after some time of doing other things and figured it out instantly.