Search Unity

Floating Damage Numbers Not Appearing Over Enemies

Discussion in 'Scripting' started by AJLeonard, Mar 14, 2021.

  1. AJLeonard

    AJLeonard

    Joined:
    Feb 2, 2021
    Posts:
    1
    I am following a simple RPG tutorial on youtube, since I'm a complete beginner. I'm trying to get floating damage numbers to appear over the enemy when the player hits them. The numbers appear in the top right hand corner of the screen every time I hit one. I made sure that the canvas render mode is set to world space. I checked other threads and the solutions were too advanced for me to understand. Here is the script for HurtEnemy:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class HurtEnemy : MonoBehaviour
    6. {
    7.     public int damageToGive;
    8.     public GameObject damageBurst;
    9.     public Transform hitPoint;
    10.     public GameObject damageNumber;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.        
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.  
    22.     }      
    23.    
    24.     void OnTriggerEnter2D(Collider2D other)
    25.     {
    26.         if (other.gameObject.tag == "Enemy")
    27.         {
    28.             //Destroy(other.gameObject);
    29.             other.gameObject.GetComponent<EnemyHealthManager>().HurtEnemy(damageToGive);
    30.             Instantiate(damageBurst, hitPoint.position, hitPoint.rotation);
    31.             var clone = (GameObject) Instantiate(damageNumber, hitPoint.position, Quaternion.Euler(Vector3.zero));
    32.             clone.GetComponent<FloatingNumbers>().damageNumber = damageToGive;
    33.             clone.transform.position = new Vector2(hitPoint.position.x, hitPoint.position.y);
    34.         }
    35.     }
    36.  
    37. }
    38.  
    And the scriptI have for FloatingNumbers:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class FloatingNumbers : MonoBehaviour
    7. {
    8.     public float moveSpeed;
    9.     public int damageNumber;
    10.     public Text displayNumber;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.        
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         displayNumber.text = "" + damageNumber;
    22.         transform.position = new Vector3(transform.position.x, transform.position.y + (moveSpeed * Time.deltaTime), transform.position.z);
    23.     }
    24. }
    I copied the tutorial exactly, and it works in the video, but I feel like I've tried everything and I can't get it to work.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,740
    It sounds like there is a step missing. Specifically I think line 33 has to be some kind of computation to take it from the world coordinates in
    hitPoint.position
    into the scale mode of your canvas.

    The Camera has functions to get you into screen space, and this class has functions to get you into Canvas space.

    https://docs.unity3d.com/ScriptReference/RectTransformUtility.html