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
0
Question by Blink · Sep 20, 2012 at 04:28 PM · guiplayerenemyhealth

Taking a hit

I am trying to get my enemy to reduce the amount of health the player has when it comes within a certain distance. I have wrote this script that tells me how far away I am from the enemy, and if I get too close the console says "enemy attack". The script also gives me a little GUI of the health that I am wanting to change also when the enemy is too close to the player. I have got so far but Im not sure how to reduce the amount of health the player has at line 12, can anyone give me some pointers?

This is what I have so far but It is not working can you help me out?

 var fullHealth : int = 100;
 var curHealth : int = 100;
 
 function OnCollisionEnter(collision: Collision) {
 
 if(collision.gameObject.tag == "Enemy");
   fullHealth -= 10;
   print ("hit");
 }
 
 function Update () {
 
 if(curHealth >= fullHealth){
     curHealth = fullHealth;
 }
 
   if(fullHealth <= 0){
     fullHealth = 0;
     Debug.Log("You are Dead");
 }
 }
 
 function OnGUI() {
     GUI.Label (Rect (25, 40, 100, 20), "Health = "+fullHealth);
 }
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by fafase · Sep 20, 2012 at 04:42 PM

Weird, youh ave most of it except the easy part:

 if(dist < 1.6){
     curHealth -= damage;
 }
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 fafase · Sep 21, 2012 at 05:12 AM 0
Share

Well, here is a quite simple way but maybe not the best:

 var health:int =100;

 function Update () {
  if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))InvokeRepeating("Damage",0.01f,1.0f);
  else if (Input.Get$$anonymous$$eyUp($$anonymous$$eyCode.Space))CancelInvoke();    
 }

 function Damage(){
   health-=10;
   print(health);
 }

InvokeRepeating calls the first argument after the second argument of time. In my case, I call Damage after 0.01s (0.00s messes up the function, sounds like a bug). The third parameter is the frequency of the call, every second. When releasing the button I cancel the invoke.

And if you want to have the code style in the comment, you need to press space bar 4 times.

avatar image
0

Answer by Griffo · Sep 20, 2012 at 05:00 PM

Try this, Not tested.

Make a square colour texture about 64 x 64 a drag it into the healthTexture in the inspector.

 #pragma strict
 
 var healthTexture : Texture2D;            // Texture to use for the health graphics
 var curHealth : int = 100;
 var enemy : Transform;
 private var adjust : int;
 private var gapTest = true;
 
 function Update () {
 
   if(enemy){
   var dist = Vector3.Distance(enemy.position, transform.position);
     print ("Distance to enemy: " + dist);
 }
 
   if(gapTest && dist < 1.6){
   print ("enemy attack");
  curHealth = curHealth - 1;
  GapSetHealth();
 }
 
 
 
   if(curHealth >= fullHealth){
     curHealth = fullHealth;
 }
 
   if(curHealth <= 0){
     curHealth = 0;
     Debug.Log("You are Dead");
   }
 }
 
 function OnGUI () {
 
 adjust = curHealth * 3;
 
 // Place the GUI at the top of the screen
 GUI.BeginGroup(Rect(Screen.width / 2 - 150,Screen.height - Screen.height + 10,adjust,15));
 GUI.DrawTexture(Rect(0,0,290,15), healthTexture);
 GUI.EndGroup();
 }
 
 function GapSetHealth(){
 
     gapTest = false;
     yield WaitForSeconds (0.02); // Gap size for the time to deduct health
     gapTest = true;
 }
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 AlucardJay · Sep 21, 2012 at 10:15 PM

original post

You need to decide how often (how many seconds between each attack) that the enemy is attacking, then use a counter and add to it every Update. When the counter reaches attack time, deduct health and reset the counter. =]

 var attackTimer : float = 0.0;
 var attackNow : float = 0.1; // 1 every 1/10 of a sec, or 10 every sec.
 
 function Update()
 {
     if ( attackTimer > attackNow )
     {
         // deduct health
         // reset Timer
         attackTimer = 0.0;
     }
     attackTimer += Time.deltaTime;
 }


in response to : Ive looked at a few different ways of making a timer but there not making any sense to me, could you help me out?

I personally have seen 2 types of timer/counter. The first is as in my comment, with a 'counter' and a maximum value.

 var counter : float = 0.0;
 var counterMax : float = 5.0;
 
 function Update()
 {
     if ( counter > counterMax )
     {
        // do stuff
        // reset Timer
        counter = 0.0;
     }
     // increment timer over time
     counter += Time.deltaTime;
     
     // increment counter with an inter value (like counting frames)
     // counter ++; // like this, counter can be an integer
 }


the other method is using Time.time itself, but it still need an increment value to work.

 var counter : float = 0.0;
 var counterDelay : float = 5.0;
 
 function Update()
 {
     if ( Time.time > counter )
     {
        // do stuff
        // reset Timer
        counter = Time.time + counterDelay;
     }
 }

Comment
Add comment · Show 2 · 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 AlucardJay · Sep 21, 2012 at 10:23 PM 0
Share

Blink :

This is what Ive got but Im getting tons of errors so I guess Its wrong, sorry for the formatting it only happens to me.

var fullHealth : int = 100; var curHealth : int = 100; var enemy : Transform; var counter : float = 0.0; var counter$$anonymous$$ax : float = 5.0;

function Update () {

if (counter > counter$$anonymous$$ax){ attack(); counter = 0.0; } // increment timer over time counter += Time.deltaTime;

// increment counter with an inter value (like counting frames) // counter ++; // like this, counter can be an integer

if(enemy){ var dist = Vector3.Distance(enemy.position, transform.position); print ("Distance to enemy: " + dist); }

function attack () {

if(dist < 1.6){ fullHealth -= 10; print ("enemy attack"); }

}

if(curHealth >= fullHealth){ curHealth = fullHealth; }

if(fullHealth <= 0){ fullHealth = 0; Debug.Log("You are Dead"); } }

function OnGUI() { GUI.Label (Rect (25, 40, 100, 20), "Health = "+fullHealth); }

avatar image AlucardJay · Sep 21, 2012 at 10:24 PM 0
Share
 var fullHealth : int = 100; 
 var curHealth : int = 100; 
 var enemy : Transform; 
 var counter : float = 0.0; 
 var counter$$anonymous$$ax : float = 5.0;
 
 var dist : float = 1000.0;
 
 function Update () 
 {
 
     if (counter > counter$$anonymous$$ax)
     { 
         attack(); 
         counter = 0.0; 
     } 
     // increment timer over time 
     counter += Time.deltaTime;
     
     if(enemy)
     { 
         dist = Vector3.Distance(enemy.position, transform.position); 
         print ("Distance to enemy: " + dist); 
     }
 
     if (curHealth >= fullHealth)
     { 
         curHealth = fullHealth; 
     }
     
     if(fullHealth <= 0)
     { 
         fullHealth = 0; 
         Debug.Log("You are Dead"); 
     } 
 }
 
 function attack () 
 {
 
     if ( dist < 1.6 )
     {
         fullHealth -= 10; 
         print ("enemy attack"); 
     }
 
 }
 
 function OnGUI() 
 { 
     GUI.Label (Rect (25, 40, 100, 20), "Health = " + fullHealth ); 
 }

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Deduct health on collision 2 Answers

Restart with GUIText 2 Answers

Adding a counter? 1 Answer

Setting Scroll View Width GUILayout 1 Answer

Need Help About Health,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