Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
-1
Question by hugemaggot · Dec 07, 2010 at 04:28 PM · enemydamage

can anyone write me a enemy damage script?

ive been trying to get my to die when i shoot it but im hopeless at scripting so could anyone write me a script that when i shoot my enemy a few times it will disapear?

Comment
Add comment · Show 3
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 The_r0nin · Dec 07, 2010 at 04:32 PM 0
Share

This requires more than one script. First, the player script that either instantiates your bullet object or raycasts your bullet hit location. Then you'll need to message the script on your enemy/target to transmit damage and have the enemy script calculate the damage acquired and start the death animation (or destroy the enemy object) if the damage exceeds your preset limit. That isn't coding "a script". Try breaking the necessary actions into steps and coding each step. You'll learn more that way...

avatar image Anton Petrov · Dec 07, 2010 at 04:33 PM 0
Share

It is a waste of time writing a script without knowing your game classes and other details. This question looks like a joke.

avatar image n8 · Dec 07, 2010 at 04:45 PM 0
Share

there is never a better time than the present to learn how to code!

3 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Statement · Dec 07, 2010 at 04:44 PM

In general you can do this:

  1. Wait for a mouse click.
  2. Create a ray that travels from your camera in the direction it's facing.
  3. Check the first thing it hits, is it your enemy? If so;
    1. Subtract a health point from enemy.
    2. If enemy health reaches zero, destroy it.

Shooter.cs - put this on your camera.

using UnityEngine;

public class Shooter : MonoBehaviour { void Update ( ) { // 1. Wait for a mouse click. if ( Input.GetButtonDown( "Fire1" ) ) { Shoot( ); } }

 void Shoot ( )
 {
     // 2. Create a ray that travels from your camera 
     //    in the direction it's facing.
     Ray ray = new Ray( transform.position, transform.forward );
     RaycastHit hit;
     if ( Physics.Raycast( ray, out hit ) )
     {
         // 3. Check the first thing it hits, is it your enemy?
         //    If so (actually there is nothing defined as enemy);
         hit.transform.SendMessage( "OnBullet",
             SendMessageOptions.DontRequireReceiver );
     }
 }

}

Shootable.cs - put this on your enemy with a collider attached to it.

using UnityEngine;

public class Shootable : MonoBehaviour { public int health = 5;

 void OnBullet ( )
 {
     Damage( );
 }

 void Damage ( )
 {
     // 3.1. Subtract a health point from enemy.
     health = health - 1;

     // 3.2. If enemy health reaches zero, destroy it.
     if ( health == 0 )
     {
         Kill( );
     }
 }

 void Kill ( )
 {
     Destroy( gameObject );
 }

}

Hopefully this skeleton will get you adding effects on your own. Pick up coding. People won't endlessly hand you scripts. Though I know the frustration if you're an artist for example and need code, or the other way around.

Comment
Add comment · Show 1 · 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 fireDude67 · Dec 24, 2010 at 06:28 AM 0
Share

Thanks for showing me Send$$anonymous$$essage()... +1

avatar image
0

Answer by omegamontage · Mar 12, 2012 at 12:42 AM

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);
 }

}

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 Eddieg26 · Feb 23, 2013 at 01:58 AM

 var health : float = 100.0;
 var maxHealth : float = 100.0;
 var healthBar : float =Screen.width/2
 
 function Update(){
   AddjustHealth(0);
   if(health < 0)
     health = 0;
   if(maxHealth < 0)
     maxHealth = 0;
 
 }
 
 function OnGUI(){
      GUI.Box(Rect(10,10,healthBar *(health / maxHealth),health);
 }
 
 function AddjustHealth(adj : int){
      health += adj;
 }
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

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

2 People are following this question.

avatar image avatar image

Related Questions

Detect enemy on animation 1 Answer

Temporary invulnerability in a platformer 1 Answer

Collision Damage 1 Answer

(C#) Enemy health and take damage from bullets 1 Answer

How to have an NPC with multiple parts detect damage 1 Answer


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