x


Redirected from FPS TUTORIAL: AI robot not taking damage.

[Closed] FPSTutorial: AI robot not taking any damage

Hi! This is a followup to the last question. I have used all of the official tutorial resources and PDF document. But, I just cant get the robot to take any damage! It will first run in place the other way from me, then when I shoot it, it doesn't die! I've tried to add a box collider and numerous things, but it just doesn't do a darn thing! Thank you! Oh yeah, here's my script:

var hitPoints = 100.0;
var deadReplacement : Transform;
var dieSound : AudioClip;

function ApplyDamage (damage : float) {
    // We already have less than 0 hitpoints, maybe we got killed already?
    if (hitPoints <= 0.0)
       return;

    hitPoints -= damage;
    if (hitPoints <= 0.0)
    {
       Detonate();
    }
}

function Detonate () {
    // Destroy ourselves
    Destroy(gameObject);

    // Play a dying audio clip
    if (dieSound)
       AudioSource.PlayClipAtPoint(dieSound, transform.position);

    // Replace ourselves with the dead body
    if (deadReplacement) {
       var dead : Transform = Instantiate(deadReplacement, transform.position, transform.rotation);

       // Copy position & rotation from the old hierarchy into the dead replacement
       CopyTransformsRecurse(transform, dead);
    }
}

static function CopyTransformsRecurse (src : Transform,  dst : Transform) {
    dst.position = src.position;
    dst.rotation = src.rotation;

    for (var child : Transform in dst) {
       // Match the transform with the same name
       var curSrc = src.Find(child.name);
       if (curSrc)
         CopyTransformsRecurse(curSrc, child);
    }
}
more ▼

asked Jul 21 '11 at 03:28 PM

goosoodude gravatar image

goosoodude
21 12 22 22

Everything should work properly right off the bat. Have you changed anything? Try re-downloading and opening the project.

Jul 21 '11 at 03:56 PM Chris D

so what i do is delete the first one? in the original script or the script in the other quesiton?

Jul 23 '11 at 07:29 PM goosoodude
(comments are locked)
10|3000 characters needed characters left

The question has been closed Jul 23 '11 at 07:42 PM by Jason B for the following reason:

Too subjective and argumentative


2 answers: sort voted first
    if (hitPoints <= 0.0)
    return;

This looks like it would be the problem. You're checking to see if(hitPoints <= 0.0) in two different parts of the code. What I copy/pasted above is the first check. When this checks and finds that you have no health left, it will return from the function and never perform the second check. Detonate will never be called.

more ▼

answered Jul 22 '11 at 07:31 PM

Kith gravatar image

Kith
526 27 28 38

The first if avoids the enemy to die again; if hitPoints>0 it's alive and can be hit. After receiving the damage, the second if checks if this damage killed the enemy.

Jul 23 '11 at 07:25 PM aldonaletto
(comments are locked)
10|3000 characters needed characters left

so, i need help, can you please post the whole code the way it is supposed to be?!?!?!?!?!?

more ▼

answered Jul 22 '11 at 08:26 PM

goosoodude gravatar image

goosoodude
21 12 22 22

Please do not post comments or questions as answers. @Kith has provided what looks like the correct solution; take a second to try to parse the meaning out of his string of words. If you can't figure it out, consider trying a more basic programming tutorial.

Jul 22 '11 at 08:32 PM Chris D

seriously, please! I just need the script. I am not a very good scripter!

Jul 22 '11 at 08:34 PM goosoodude

...@Kith has literally listed the answer; just eliminate that code. I understand you're not the greatest scripter but you've gotta think about these things if you want to improve. Otherwise you're going to be here for every problem you have.

Jul 22 '11 at 08:49 PM Chris D

I'm sorry to burst your bubble, but I know how to move, I know a lot of things about Unity3D. Oh yeah, that was not the answer! I deleted that part of the code and it said "All compiler errors have to be fixed to enter playmode!"

Jul 22 '11 at 09:20 PM goosoodude

You're making it very difficult for us to help you. "All compiler errors have to be fixed to enter play mode". Yes, we know this. Can you tell us what your error is? Open up your console and see what it says. I'm sorry, but unless you're willing to pay me, I'm not going to write the script for you. If you're serious about learning how to program well, you're going to have to increase your problem-solving skills. I'll be happy to point you in the right direction, but this isn't a place for you to ask people to write your code for you.

So, what is the error? Because I assure you, the problem you were having before was due to that if statement you deleted.

Jul 22 '11 at 09:43 PM Kith
(comments are locked)
10|3000 characters needed characters left

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:

x248
x82

asked: Jul 21 '11 at 03:28 PM

Seen: 1827 times

Last Updated: Jul 23 '11 at 07:43 PM