Hey guys just found a cool script that does almost entirely what I want it to do except 1 thing. So the script is for picking up objects losing health when hit by other objects and gaining health when getting hit by other objects(its money enemy and health)
Problem is when I hit an enemy I do not want the enemy to disappear
#pragma strict
var Health:float=100;
var maxHealth:float=100;
var maxBAR:float=100;
var HealthBarLength:float;
// This is the player's money
var Money:int=0;
function OnGUI()
{
// This code creates the health bar at the coordinates 10,10
GUI.Box(new Rect(10,10,HealthBarLength,25), "");
// This code determines the length of the health bar
HealthBarLength=Health*maxBAR/maxHealth;
}
function ChangeHealth(Change:float)
{
// This line will take whatever value is passed to this function and add it to curHP.
Health+=Change;
// This if statement ensures that we don't go over the max health
if(Health>maxHealth)
{
Health=100;
}
// This if statement is to check if the player has died
if(Health<=0)
{
// Die
Debug.Log("Player has died!");
}
}
// This function checks if the player has entered a trigger
function OnTriggerEnter(other:Collider)
{
// The switch statement checks what tag the other gameobject is, and reacts accordingly.
switch(other.gameObject.tag)
{
case "Health":
ChangeHealth(25);
break;
case "enemy":
ChangeHealth(-25);
break;
case "Money":
Money+=25;
break;
}
// Finally, this line destroys the gameObject the player collided with.
Destroy(other.gameObject);
}
probably the only important thing in my script for this scenario is the last function here what I am trying to do is destroy all objects upon collision except enemy is there an easy way of doing this?
if (other.gameObject.tag != "enemy")
Destroy(other.gameObject);
Logic is wonderful is it not?
Another alternative is (my approach)
function OnTriggerEnter(other:Collider)
{
// The switch statement checks what tag the other gameobject is, and reacts accordingly.
switch(other.gameObject.tag)
{
case "Health":
ChangeHealth(25);
Destroy(other.gameObject);
break;
case "enemy":
ChangeHealth(-25);
break;
case "Money":
Money+=25;
Destroy(other.gameObject);
break;
default:
Destroy(other.gameObject); //Remove this to stop it destroying all other objects except the ones above.
break;
}
}
Your solutions are so obvious lol cannot believe I didn’t think of it like that but this has lead to another issue:o I expected when I did this I would collide with the enemy lose health but not be able to move through them:p any way to fix this? I am using is trigger (disabled gravity so my enemy would not fall through the ground is there something I need to tick for me not to go through my enemies lol:P? or is it something to do with my script?
Triggers on their own don’t normally collide, which is why they are triggers. The documentation says this:
IsTrigger: If enabled, this Collider is used for triggering events, and is ignored by the physics engine.
That includes collisions. This makes them great for items and events. Enemies, though, are not usually triggers. They should have normal colliders and use OnCollisionEnter, or have child objects that have the triggers instead.