Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

DmgText Instantiate position

Discussion in 'Scripting' started by beeflilly, Apr 15, 2020.

  1. beeflilly

    beeflilly

    Joined:
    Apr 1, 2020
    Posts:
    6
    Hi there!

    I'm currently messing around with an fps build and have now hit a mental block when dealing with the floating damage text and getting it to appear where I want it to appear. The video link below shows the issue that I am having with the damage appearing from the bottom of my enemies.

    https://imgur.com/oXJNQaM - Video of issue (sorry for the quality. The issue is easier to see on the green enemy)

    I have the damage text instantiating, but my issue is the Y position will not change. I have created an offset in my 'dmgTextScript', but the Y value of the offset is being completely ignored, while the X and Z values of the offset work fine and do affect the position of the dmgText when testing. Any ideas on what could be causing this?

    The dmgTextPrefab does have an animation on it, is that an issue?

    Code (CSharp):
    1. public class dmgTextScript : MonoBehaviour
    2. {
    3.     public float destroyTime = 2f;
    4.     public Vector3 offset = new Vector3(0, 2, 0);
    5.  
    6.  
    7.     void Start()
    8.     {
    9.         Destroy(gameObject, destroyTime);
    10.        
    11.         transform.localPosition += offset;
    12.     }
    13.  
    14. }
    Here is the code that instantiates the dmgText:

    Code (CSharp):
    1. public void TakeDamage(int amount)
    2.     {
    3.         takingDamage = true;    
    4.         health -= amount;
    5.         healthBar.SetHealth(health);
    6.         if (dmgTextPrefab && health > 0)
    7.         {
    8.             ShowDmgText(amount);
    9.         }
    10.  
    11.         if (health <= 0)
    12.         {
    13.             health = 0;
    14.             Die();
    15.         }
    16.        
    17.     }
    18.  
    19.     void ShowDmgText (int amount)
    20.     {
    21.         var dmg = Instantiate(dmgTextPrefab, transform.position, Quaternion.LookRotation(Camera.main.transform.forward), transform);
    22.         dmg.GetComponent<TextMesh>().text = amount.ToString();
    23.     }
    24. }
    Thanks in advance! Cheers!
     
  2. Serinx

    Serinx

    Joined:
    Mar 31, 2014
    Posts:
    785
    What part of the prefab are you animating? If you're animating the root objects Y position then that'll be the issue.

    You might need to add a parent object to the object you're animating and set the position of the parent object instead.
     
    beeflilly likes this.
  3. beeflilly

    beeflilly

    Joined:
    Apr 1, 2020
    Posts:
    6
    Thank you so much! That was my issue!
     
    Serinx likes this.