• 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
0
Question by pickle chips · Jul 25, 2013 at 01:51 AM · guichangeclicktarget

Hitmarker appears on both targets

So I know what my problem is, but after hours of trying to fix it I can't think of a way to do it. So when I attack the enemy (or target), the GUI is drawn on the target saying the amount of damage. But if I change targets (by clicking on a different enemy) while the hitmarker is still being shown, it then shows on the new target, not the one that actually took the damage. How can I prevent this?

Here is my code: public void showHitMarker() { Vector3 v3pos = Vector3.zero;

         if (target) {
             v3pos = target.transform.position;
             v3pos = Camera.main.WorldToScreenPoint(v3pos);
             v3pos.y += 25;
             v3pos.y = Screen.height - v3pos.y;
             v3pos.x -= 25;
         }
 
         
         if (showDamage > 0) {
             if (!target)
                 showDamage = 0;
             
             GUI.Label(new Rect(v3pos.x, v3pos.y, 50, 50), damage.ToString(), hitMarker);
             showDamage -= Time.deltaTime;
         }
     }

As you can see, when i switch to a new target it would make sense that it is drawn on the new one. I've tried storing the right target to draw it on in a variable but I can't figure out how to do that properly...

Any ideas? Thanks

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

2 Replies

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

Answer by oddeven · Jul 25, 2013 at 02:23 AM

I would suggest restructuring things a bit. Think about creating a HitDamageScript that you attach to objects when you hit them. The script would render the damage for some time and then destroy itself. You can move the render logic you have now to OnGUI of HitDamageScript. In your hit detection logic add something like:

 // assuming target is a GameObject
 HitDamageScript script = target.AddComponent<HitDamageScript>();
 script.SetDamage(X); // where X is the amount of damage caused
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 pickle chips · Jul 25, 2013 at 12:06 PM 0
Share

I like this idea, I'll try it out tonight

avatar image pickle chips · Jul 25, 2013 at 10:44 PM 0
Share

I didn't use your exact idea, but I did reconstruct it a bit, and i will accept your answer because it's what gave me the idea. I just put the hitmarker part in the enemyHealth script and not in the playerAttack script, so when the enemy takes damage it shows the hitmarker.

avatar image oddeven · Jul 26, 2013 at 01:20 AM 0
Share

very nice, glad to help!

avatar image
0

Answer by Trey3Stone · Jul 25, 2013 at 02:19 AM

Try setting showDamage = 0 at the point where you change target.

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

17 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 avatar image avatar image avatar image

Related Questions

Passing values between scripts 5 Answers

How to change Target of camera from Standard Assets 1 Answer

GUI Overlay Display 0 Answers

Problem with HitMaker? 0 Answers

How do I script a GUI Texture as a button, if it's being called from a public variable? [C#] 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges