hi guys i need help in damage script.i have a capsule object which is known as player, and a sphere object as enemy.how to i make the sphere object destroy itself when collide with the capsule and taking away one health points from the capsule.
here is my capsule collide script:
var Lives = 1;
var HealthControl;
function OnTriggerEnter( hit : Collider )
{
if(hit.gameObject.tag == “fallout”)
{
dead = true;
//substract life here
HealthControl.Lives -= 1;
First, make sure your OnTriggerEnter is triggered by putting a print
print("OnTriggerEnter:" + hit.gameObject.tag);
Then you’ll know 2 things, if your triggers are setup correctly and if the tags are setup correctly. Your destroy seems fine and your health stuff you haven’t explain how they work.
If your OnTriggerEnter is not triggered it can mean a couple things, make sure your enemy has a collider with the “IsTrigger” flag set to true
and make sure your player has an actual physics component, not just a trigger.
I would say, first define your health or lives (what ever you are using) as a static variable on your player script(The scripts which is attached to your player). Lets say your scripts name is Player and your static variable name is HEALTH, ok now you can acces that variable with this script and reduce the amount by a desirable amount. Attach this to your sphere.
function OnControllerColliderHit(hit : ControllerColliderHit)
{
// Player is your player simply…
if(hit.gameObject.tag == “Player”)
{
// this destroys the sphere and substracts -1 HEALTH from your Player script. You can print this health later by the next function.
Destroy(hit.gameObject);
Player.HEALTH --;
// This basically displays your new health amount on the screen. And there you go, wallah
So now, you can change the names of player and YOUR_HEALTH_DISPLAY_GUI_NAME things accordingly so that you have your desired results. In your script that contains the HEALTH static variable, the one on your player, you can do whatver you want with that later on. Like