x


Calling a variable

Hi this may sound like a very dumb question but I am trying to get the explosion to be made at the player location but only when the health reaches 0, here isi the script.

var explosion : GameObject;
var playerHealth = 100;
var damage = 10;

function OnCollisionEnter(collision : Collision)
{
    var contact : ContactPoint = collision.contacts[0];
    var rotation = Quaternion.FromToRotation(Vector3.up, contact.normal);
    var playerDead = Instantiate (explosion, contact.point, rotation);

   if(collision.gameObject.tag=="objectA" || collision.gameObject.tag=="objectB" || collision.gameObject.tag=="objectC") 
   {
        playerHealth -= damage;
   }
}

function Update()
{
    if(playerHealth <= 0)
    {
                //want to call the playerDead variable here if possible
       Destroy(gameObject);
    }
}

Thank you

more ▼

asked Nov 07 '11 at 01:57 PM

exchangeSh9 gravatar image

exchangeSh9
1 2 2 2

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

You just need to rearrange things a little bit.

In your OnCollision-

if(collision.gameObject.tag=="objectA" || collision.gameObject.tag=="objectB" || collision.gameObject.tag=="objectC") 
{
     playerHealth -= damage;

     if(playerHealth <= 0)
     {
         // Instantiate the explosion, delete the object
     }
}

Then just get rid of the thing in Update (since it is now unnecessary)

more ▼

answered Nov 07 '11 at 02:03 PM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

Dude thank you for the help, I made those adjustments and it is working now :) also cheers Ludiares for the help mate

Nov 07 '11 at 11:32 PM exchangeSh9
(comments are locked)
10|3000 characters needed characters left

Try Instantiting the explosion in the if block, not inside the OnCollisionEnter function

more ▼

answered Nov 07 '11 at 02:03 PM

Ludiares gravatar image

Ludiares
210 2 3

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2083
x1670
x819
x477
x161

asked: Nov 07 '11 at 01:57 PM

Seen: 1010 times

Last Updated: Nov 07 '11 at 11:32 PM