- Home /
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
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
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.
Answer by Trey3Stone · Jul 25, 2013 at 02:19 AM
Try setting showDamage = 0
at the point where you change target
.
Your answer
Follow this Question
Related Questions
Passing values between scripts 5 Answers
Moving Target Circle 2 Answers
click on GUI button goes through to a collider... 0 Answers
FBX Animation Import 2 Answers
make GUI not overlap? 0 Answers