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 Sefcio · Jun 21, 2015 at 11:20 AM · attackbegginerhphealth

Atack and HP 'universal' scripts, begginer need guidance.

I want to create a script that can work both on my player units, and also on my enemy units. The problem is that the units receive damage, but the value of it is set on themselves. For example i got a player unit with dmg set to 10, and if it encounters enemy this unit is losing 10 hp every 'hit'. I tried bunch of thing with tags etc, but nothing was working.

Since I can't figure it out by my self if there is a way to do that without separating the script for 'player atk' and 'enemyatk' as well as 'playerhp' and 'enemyhp', here is my code to atk, and hp script, please at least guide me on the right track.

ATK SCRIPT:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class Atk : MonoBehaviour {
 
     public float timeBetweenAttacks = 0.5f;
     public int minDmg;
     public int maxDmg;
 
     GameObject mob;
     GameObject player;
     
     private Health health;
 
     bool playerInRange;
     bool mobInRange;
     float timer;
     
     void Awake ()
     {
         player = GameObject.FindGameObjectWithTag ("Tower");
         mob = GameObject.FindGameObjectWithTag ("Mobs");
 
         health = GetComponent<Health> ();
     }
     
     
     void OnTriggerEnter (Collider other)
     {
         if(other.gameObject == player)
         {
             playerInRange = true;
         }
         if(other.gameObject == mob)
         {
             mobInRange = true;
         }
     }
 
 
     void OnTriggerExit (Collider other)
     {
         if(other.gameObject == player)
         {
             playerInRange = false;
         }
         if(other.gameObject == mob)
         {
             mobInRange = false;
         }
     }
 
 
     void Update ()
     {
 
         timer += Time.deltaTime;
 
         if (timer >= timeBetweenAttacks && playerInRange == true && health.currentHealth > 0)
         {
             Attack ();
         }
         if (timer >= timeBetweenAttacks && mobInRange == true && health.currentHealth > 0)
         {
             Attack ();
         }
     }
 
     
     void Attack ()
     {
         timer = 0f;
 
         if(health.currentHealth > 0)
         {
             health.TakeDamage (Random.Range (minDmg, maxDmg));
         }
     }
 }

HP SCRIPT

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class Health : MonoBehaviour {
 
     public int startingHealth = 100;
     public int currentHealth;
 
     bool isDead;
 
 
     void Awake ()
     {
         currentHealth = startingHealth;
 //        healthSlider.maxValue = startingHealth;
     }
     
     public void TakeDamage (int amount)
     {
         currentHealth -= amount;
 
 //        healthSlider.value = currentHealth;
         
         if(currentHealth <= 0 && !isDead)
         {
             Destroyed ();
         }
     }
 
     void Destroyed ()
     {
         isDead = true;
         Destroy(this.gameObject);
     }
     
 }
Comment
Add comment · Show 1
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 Socapex · Jun 21, 2015 at 12:42 PM 0
Share

Well, the real question is why do you need to have a global script that has access to all of these things? $$anonymous$$aybe you are looking at the problem the wrong way?

Anyhow, having each object be "independent" is good :) So what you can do, is ins$$anonymous$$d of accessing all of your players and enemies, have them all link back to 1 "master" script, and tell that script to update it's values.

What are you trying to do exactly?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Mark Gossage · Jun 21, 2015 at 02:23 PM

I'm thinking about how I would go at the problem, you didn't explain the type of game & that makes a difference to the work. Drop me a comment on the kind of combat & I will adjust the solution.

The health script is ok, I would probably change the Destroyed() function to be called by a SendMessage(), as that makes it more flexible. Its the Atk script which needs to changed.

Here is a general summary of what I would do:

  1. Have a tag (player/enemy) and the health script on the game objects.

  2. The weapon (sword/gun/bullets) can have a script which can look for the health script and damage it when needed.

  3. Since players & enemies usually die differently, I might have another script to handle the death, which can be called by the health script upon the death.

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 Sefcio · Jun 21, 2015 at 04:28 PM 0
Share

The game will be a hybrid of RTS and TD game.

I don't have all the animations(I will have 242 completely different units) for death/attack and so on, so I just put that 'Destroy' to simplify this for some time. I wanted to 'simplify' the attack script as well by making it universal for all units, but I just complicated it ins$$anonymous$$d...

I think I will just separate the attack script for towers(player) and mobs(enemy).

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Player Health Goes Down Too Fast/Too Much 0 Answers

Player attack script + enemy health 1 Answer

MOB HP - Subtract on Hit if Near 1 Answer

Attack,Health and enemy health. 1 Answer

Two player cube battle can't attack at the same game. 0 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