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
3
Question by Frix · Nov 25, 2012 at 03:13 PM · 2dpositionpopupvar

How can I add text at a point where an enemy dies?

I haven't slept in 2 days pretty much so I'm finding it hard to think. I'm also only a beginner really.

I have a 2D game, and when you kill an enemy, it adds to your current gold. How would I have a 'popup' of the gold gained where the enemy died?

Say, you kill an enemy, it explodes, and right where it exploded, it would show "+[gold]". That +[gold] would disappear after about a second or two, and it would do it each time an enemy died. I have the gold earned on kill stored in an int, I just don't know how to get the position and how to make it disappear after a second and stuff.

edit: I wanted to head along the lines of using the GUI. Well I think that might be the only way so yeah. I don't know. I can't think I'm so tired.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by BiG · Nov 25, 2012 at 03:45 PM

Personally, I use the following method:

Create a GUIText for the popup message (say "+ 100 Gold"). I assume the text to be static for semplicity, but you can modify the content of the text at runtime;

Destroy the popup message after a certain amount of time, with a script like this (attached to the GUIText):

 var destroyTime=5;
 function Start () {
    yield WaitForSeconds(destroyTime);
    Destroy(gameObject);
 }

If you want to add a good effect, make also the message move vertically, before destroing it:

 var speed = 90;
 function Update () {
     var y=Time.deltaTime*speed;
     transform.Translate(0,y,0);
 }

Now, make that GUIText a prefab (I will call it "Score_Text";)

Now, the interesting part: let's make the enemy die and show the respective gold:

 var life=10;
 var score_e=100;
 var Score_Text : Transform; //This is the GUIText prefab that we created a bit ago
 var scoreScaling : Vector3; //It's better that the message will appear a little shifted, respect to the enemy, say Vector3(5,0,5)
 
 function OnCollisionEnter (theCollision : Collision) {    
     var player_damage : int;
     if(theCollision.gameObject.tag == "Player"){
        life -= player_damage;
        if (life<=0){
           GameObject.Find("Score").GetComponent(UpdateScore).score+=score_e;
           Instantiate(Score_Text, transform.position+scoreScaling, transform.rotation); 
           Destroy(gameObject);
        }
     }
 }

The script above is a complete example of a "killing-enemy" one, Obviously, it's just an example, and the interesting part, for you, is the line:

 Instantiate(Score_Text, transform.position+scoreScaling, transform.rotation); 

I hope that you will find this a bit useful to start.

Comment
Add comment · Show 15 · 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 Frix · Nov 25, 2012 at 03:49 PM 0
Share

Right, that's too much for my sleep deprived $$anonymous$$d to handle right now, but the first two bits look good. I was going to make that little effect as well, so you just saved me some time thankyou. I'll take a look at this tomorrow and get back to you, thanks for that.

avatar image BiG · Nov 25, 2012 at 03:52 PM 0
Share

No problem, let us know.

avatar image Frix · Nov 26, 2012 at 02:18 PM 0
Share

I've tried everything with that script, and it keeps on giving me the error: 'UnassignedReferenceException: The Variable goldText of 'bulletScript' has not been assigned.'

'goldText' is the prefab name and the variable name, same as your Score_Text. Why wont this work? I even tried assigning it in the inspector and it still gives me that error.

avatar image BiG · Nov 26, 2012 at 03:03 PM 0
Share

Have you assigned the last script to the enemy AND drag 'n dropped the GUIText prefab in the enemy's Inspector, on the goldText variable? From the error, it seems that you have a "blank slot" in the goldText value, somewhere.

avatar image Michael Covert · Nov 28, 2012 at 08:02 PM 1
Share

Basically, GUIText position can be interpereted as relative position across the screen - this means a position of something outside of the (0,0) -> (1,1) rectangle is most likely not going to be on your screen. Since your enemies are probably not dying within this particular area, and are most likely not perfectly matched to screen position when they do, spawning it in based on the dead unit's position directly is probably nto what you want. 3D text can be used like you're trying to use this - though you may have to make sure it faces the camera (been a while since I used them, I can't recall). Alternatively, you can try using Camera.main. WorldToScreenPoint to figure out where on your screen the dead unit is, and then use unity GUI calls to draw your text there.

Show more comments
avatar image
0

Answer by Griffo · Nov 28, 2012 at 08:42 PM

OK I'm going to tell you how I do it, when I kill an enemy the text zooms into the camera fading as it does.

1 - Create an empty game object, I call it "TextContainer" and child it to you enemy then attach the first script below to it.

2 - Now create a 3D Text and child that to the one you've just made ( TextContainer ) I call this "Text" and attach the second script below, fill in the Text Mesh field in the inspector with your font and text .. ect .. When I import fonts I set there font size to around 90 so in the inspector set the scale to X-0.02 Y-0.02 Z-0.02 and in the Text Mesh set Character Size to 0.1, Anchor to Middle Centre and Alignment to Centre.

3 - Now in the Inspector of the TextContainer turn off the game object (un-tick, top left corner)

4 - Now on you enemy add this variable

 var textContainer : GameObject;

Also add this function and call it when his/her life reaches 0

 function destroy(){
     textContainer.gameObject.SetActive(true);
     yield WaitForSeconds(3.0);    // Wait for 3 seconds then destroy the gameObject and its children
     Destroy(gameObject);
 }

5 - Now choose your enemy and in the Inspector in the Text Container slot drag the TextContainer game object you made in step 1 .. Hope this helps you out, and you may need to alter as it suits you.


1

 #pragma strict
 
 function Awake(){
 
 }
 
 function Start () {
 
 }
 
 function Update () {
 
     transform.position = Camera.main.transform.position + Vector3(0,0,0) + Camera.main.transform.forward * 1;
     transform.rotation = Camera.main.transform.rotation;
 }


2

 #pragma strict
 
 var textColor : Color;
 
 private var runOnce : boolean = true;    // Use to run Coroutine once
 private var originalScale : Vector3;
 
 function Awake(){
 
 }
 
 function Start(){
 
     renderer.material.color = textColor;
     renderer.material.color.a = 1.0;
 }
 
 function Update(){
 
     renderer.material.color.a -= 0.1 * Time.deltaTime * 5;    // Fade away text, last number dictates how fast to fade, higher = faster
     StartCoroutine(LerpScale(2.0f));                        // Time to take to Lerp to new size in seconds
 }
 
 function LerpScale(time : float){
 
 if(runOnce){
 
    var targetScale = originalScale + Vector3(0.15, 0.15, 0.15);    // New size to expand text to
    var originalTime = time;
 
    while (time > 0.0f)
    {
       time -= Time.deltaTime;
 
       transform.localScale = Vector3.Lerp(targetScale, originalScale, time / originalTime);
 
       yield;
         }
     }
     runOnce = false;
 }
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 Frix · Nov 28, 2012 at 08:43 PM 0
Share

I just solved this 5 seconds before you posted this, damn :/ Thanks for the effort though..

avatar image
0

Answer by jheiling · Nov 25, 2012 at 03:38 PM

Camera.WorldToScreenPoint gets you the position of an object in screen space. To destroy an object after some time simply use Destroy.

Comment
Add comment · Show 3 · 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 Frix · Nov 25, 2012 at 03:44 PM 0
Share

Confused. Could you give me a quick example? I looked at the document and don't really understand. Won't need anything too complicated.

And what do you mean about destroying the object? What object would I be destroying? The text? I thought that wouldn't let the gold pop up anymore though

avatar image jheiling · Nov 27, 2012 at 12:09 PM 0
Share

camera.WorldToScreenPoint(someTransform.position) will give you the screen position of someTransform. Destroy(someObject, time) will destroy someObject in time seconds. Of course you would destroy the object displaying the text...

avatar image Frix · Nov 28, 2012 at 09:15 AM 0
Share

I can't use that. It says I need a camera attached to the object.

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

14 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Checking to see if a position is occupied by a GameObject 2 Answers

How to Move an NPC from anyplace to a specific place in 2D? 1 Answer

How to get the position that is closest to a target within a given radius 0 Answers

How do I make my camera follow the player without rotating in 2D? 2 Answers

Moving child and parent 1 Answer


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