- Home /
FPSTutorial: AI robot not taking any damage!
Hello well, this is my script. I have gotten an answer to remove the if (hitPoints <= 0.0) part. But, which one? I have been asking this question alot but i never seem to get any help!
var breakpoints = 100.0;
var deadReplacement : Transform;
var dieSound : AudioClip;
function ApplyDamage (damage : float) {
// We already have less than 0 hitpoints, maybe we got killed already?
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);
}
}
Don't remove it. I'll explain what's happening in the answer.
k. so what i do is add this to the ai, or what, my machineGun object? Or the thing that contains the machineGun script, or the empty that contains the weapons script, or the main camera, or the graphics, or just the First person controller
The script above should be attached to the enemy. The other I've posted in my answer below can be attached to the First Person Controller - actually, it can be attached to any object because it doesn't use any object information (like transform, rigidbody etc.) - the only one you should not attach it to is the robot, because it will die (the script would follow it to hell!)
The if removed will not affect anything because the enemy will be destroyed anyway when hitPoints is
Answer by devilkkw · Jul 23, 2011 at 08:16 PM
if (hitPoints <= 0.0)
{
Detonate();
}
else {
hitPoints -= damage;
}
Answer by aldonaletto · Jul 23, 2011 at 08:23 PM
The script above do nothing alone: some robot part must send the message "ApplyDamage" for this to work. I suspect you don't have any script doing this, so let's create one:
function Update(){ if(Input.GetMouseButtonDown(1)){ // if the RIGHT button pressed // casts a ray from the mouse pointer var ray = Camera.main.ScreenPointToRay(Input.mousePosition); var hit: RaycastHit; if(Physics.Raycast(ray, hit)){ // if something hit, send the message hit.collider.SendMessageUpwards("ApplyDamage", 20, SendMessageOptions.DontRequireReceiver); } } }Attach this script to your player and run the game. Click the robot with the RIGHT button and a 20 points damage will be applied. After 5 clicks, the robot will die.
This script creates a ray perpendicular to the screen starting from the mouse pointer. If something is hit, it sends the message ("ApplyDamage", 20) to the hit collider owner.
The machine gun uses the same idea to apply damage. I strongly suggest that you download the FPS tutorial and follow its steps one after one.
Um...where do I add this script. Starting on which line?
You can save this as a new script and add it to the player or the camera (or to any other object in scene - EXCEPT the robot, which will die and take the script with him to hell...)
The idea (as explained above) is to cast an imaginary ray from the mouse pointer; if this ray hits something, the variable hit brings information about the object hit; we then "send the ApplyDamage message" to the object hit. In simple terms, this makes Unity search for functions called ApplyDamage in the object's scripts, and execute this function whenever it's found.
$$anonymous$$aybe you can help me with this. Go to my new question
Wait can you like get the mouse pointer to stay in the center, and turn it in to a cross hair.
Your answer
Follow this Question
Related Questions
Health Regeneration 2 Answers
Health Bar fire damage 1 Answer
Universal Damage Sytem 2 Answers
Enemy Health, Player Damage 0 Answers
Damage/Health problem 2 Answers