- Home /
how to display the damage value from a mob
hi im having a bit of a confusing time with this 1, im new to c# and unity but im learning as i go here, ive got a script for a mob to attack a player and it deducts from the players current health fine, now im trying to display the damage in a kind of floating combat text,
now ive got the text to display fine, im just unsure how to get the value that is being deducted and displaying that amount, ive commented the code as much as possible and im hoping someone can just point me in the right direction
using UnityEngine;
using System.Collections;
public class mobatt : MonoBehaviour {
public GameObject target;
public float attacktimer;
public int cooldown;
float Damagedisp;
private float scrollspeed; //scroll speed on the vertical
// Use this for initialization
void Start () {
attacktimer = ( Random.Range(1,3));
cooldown = ( Random.Range(1,3));
//---------
scrollspeed = Screen.height /2-10; //<--text scrolling
//---------
}
void OnGUI ()
{
GUI.Label ( new Rect(Screen.width/2-25, scrollspeed, 50, 50), Damagedisp.ToString()); <--this is the position of the scrolling text
}
// Update is called once per frame
void Update () {
if (attacktimer > 0)
attacktimer -= Time.deltaTime;
if (attacktimer < 0)
attacktimer = 0;
{
if (attacktimer == 0) {
attack ();
attacktimer = cooldown;
}
}
//------
scrollspeed -= Time.deltaTime*300; //<--scroll rate of the text
//------
}
private void attack() {
float distance = Vector3.Distance (target.transform.position, transform.position);
Vector3 dir = (target.transform.position - transform.position), normalized;
float direction = Vector3.Dot (dir, transform.forward);
if (distance < 2.0f) {
if (direction > 0.13) {
playerhp eh = (playerhp)target.GetComponent ("playerhp"); //this gets the value of the currenthp and the next line does the deductions
eh.adjcurhp (- Random.Range(1,5));
Damagedisp = 20; //<<<<<<---this is the part im having trouble with i cant seem to figure out what to put here, i was thinking Damagedisp = eh.adjcurhp but it doesnt seem to work
}
}
}
}
as i say i am very new to both unity and c# so i am willing to learn but obviously i need a little bit of guidance on the end bit,
thanks for any help on this 1
Answer by mwbranna · Oct 20, 2014 at 07:55 PM
Some stuff to optimize in here but I'll leave it expanded for clarity. Basically you just generate the number once and use that same value to do damage and to display instead of trying to pass the number in to "eh" and getting it back out.
int damage = Random.Range(1,5);
eh.adjcurhp(-1 * damage);
Damagedisp = damage;
SUPERB!!! short and sweet yeah i was trying to figure out a way of getting the value back from eh, i was considering looking at the health script then working the difference between current hp then the new current hp then i lost my head thinking about it, lol this again is perfect thanks very much, this kind of advice is what im always after because obviously its a perspective im not seeing at the moment
thanks again