- Home /
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);
}
}
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?
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:
Have a tag (player/enemy) and the health script on the game objects.
The weapon (sword/gun/bullets) can have a script which can look for the health script and damage it when needed.
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.
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
![](https://koobas.hobune.stream/wayback/20220613201223im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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