Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by goosoodude · Jul 23, 2011 at 08:06 PM · damagefpstutorialhealth

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);
     }
 }
Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aldonaletto · Jul 23, 2011 at 08:08 PM 0
Share

Don't remove it. I'll explain what's happening in the answer.

avatar image goosoodude · Jul 23, 2011 at 08:52 PM 0
Share

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

avatar image goosoodude · Jul 23, 2011 at 08:53 PM 0
Share

o yeah, i did download the real tutorial.zip file!

avatar image aldonaletto · Jul 24, 2011 at 12:10 AM 0
Share

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!)

avatar image aldonaletto · Jul 24, 2011 at 12:16 AM 0
Share

The if removed will not affect anything because the enemy will be destroyed anyway when hitPoints is

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by devilkkw · Jul 23, 2011 at 08:16 PM

 if (hitPoints <= 0.0)
 {
     Detonate();
 }
 else {
 hitPoints -= damage;
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image goosoodude · Sep 25, 2011 at 05:56 PM 0
Share

Um...where do I add this script. Starting on which line?

avatar image aldonaletto · Sep 25, 2011 at 06:27 PM 0
Share

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.

avatar image goosoodude · Sep 25, 2011 at 06:40 PM 0
Share

$$anonymous$$aybe you can help me with this. Go to my new question

avatar image goosoodude · Sep 25, 2011 at 06:45 PM 0
Share

Wait can you like get the mouse pointer to stay in the center, and turn it in to a cross hair.

avatar image goosoodude · Sep 25, 2011 at 08:28 PM 0
Share

Well, Problem one solved

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges