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 /
This question was closed Oct 30, 2013 at 06:03 PM by meat5000 for the following reason:

'Question' Answered.

avatar image
0
Question by Quentin · May 30, 2010 at 07:59 PM · damage

Please, I need a damage script

Hi, I've been trying to make a script to damage my NPC's they already have hitpoints and health but I can't do any damage to them. If someone could be kind enought to paste one as an anwsere or reply it would help me alot. Thatk's for your time and good luck with unity!

Yes, I am quite new to unity.

Comment
Add comment
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

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by Peter G · May 30, 2010 at 11:01 PM

See the FPS Tutorial for a detailed explanation.

A simple example.

The enemy script.

var damage = 10;

function OnCollisionEnter (col : Collision) { col.gameObject.BroadCastMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver); }

player script:

var health = 50

function ApplyDamage (damage : int) { health -= damage;

  if(health <= 0) {
      Die();
  }

function Die () { //Die and or Respawn }

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 Quentin · May 31, 2010 at 12:37 PM 0
Share

Thankyou very much, it's good to see experiance user help newer ones :)

avatar image Quentin · May 31, 2010 at 12:43 PM 0
Share

if anyone eles has a alternative script please do post :)

avatar image Peter G · May 31, 2010 at 01:02 PM 1
Share

What's wrong with this one?

avatar image Lost_C4 · Apr 10, 2013 at 09:20 PM 0
Share

nothing. i think he wants to create a library.

avatar image goten · Oct 30, 2013 at 05:31 PM 0
Share

im sorry i am a noob to scripting. where do i put the semicolon at

Show more comments
avatar image
0

Answer by Brushy · Mar 31, 2013 at 09:29 PM

 static var health : int = 0;
 var collided : boolean = false;
 var healthguitext : GUIText;
  
 function Start(){
 health = 10;
 }
  
 function OnControllerColliderHit(hit : ControllerColliderHit) {
 if(hit.collider.gameObject.tag == "(name of object here)") {
 LessDamage();
 }
 }
  
 function Update(){
 healthguitext.text = health.ToString();
 }
  
  
 function LessDamage(){
 if(!collided) {
 health--;
 collided = true;
 yield WaitForSeconds(1);
 collided = false;
 }
 }

just replace the (name of object by the name of the object you want to apply damage to.

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

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Armour / Damage System RPG-like? 2 Answers

Bullet scripting , i need help 1 Answer

Rate of health loss in trigger box. 1 Answer

Destroy enemy using raycasts 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