Need help figuring out what would be the the best way to have an rpg like dmg pop up on hit(borderlands style or Castlevania), game is in 2D.
i already have the dmg calculation stored in a variable but how do i show it on top of the enemy on hit, then fade out.
should i use GUIText or any kind of advice would be nice.
EDIT: got this so far, show the damage on screen where the enemy is
var GUIDamage : GUIText;
var GUIPrefab : GameObject;
function OnTriggerEnter (EnemyCollision : Collider)
{
if (EnemyCollision.tag == "Missile")
{
GUIDamage.text = "" + Fire.MissileDamage;
Instantiate(GUIDamage, Camera.main.WorldToViewportPoint(gameObject.transform.position), Quaternion.identity);
Destroy(GUIPrefab, 1);
}
}
but i will get an error:
Destroying assets is not permitted to avoid data loss.
If you really want to remove an asset use DestroyImmediate (theObject, true);
which i dont think it is what i want. and should this script be dragged onto each enemy to find its position, cause if i dont nothing shows up otherwise, and if i do that i cant rly delete the prefab anomore since its attached to the enemy will fix the position and fade and so on later on when i fix this 1st.
private var time_ : float;
var time_to_fade : float;
function Start ()
{
time_ = Time.time;
}
function Update ()
{
transform.Translate(Vector3(0,0.001,0));
guiText.material.color.a = Mathf.Cos((Time.time - time_)*((Mathf.PI/2)/time_to_fade));
Destroy (gameObject,time_to_fade);
}
this should do the trick for the "going up and fading" part (use a small value for the translate function, since when the position reaches 1 the text cant be seen anymore, but not too small or it wont move at all, and change the alpha value of the material color attached to the guitext)
and try attaching this to the enemy
private var GUIDamage : GameObject;
var GUIPrefab : GameObject;
function OnTriggerEnter (EnemyCollision : Collider)
{
if (EnemyCollision.tag == "Missile")
{
GUIDamage = Instantiate(GUIPrefab, Camera.main.WorldToViewportPoint(gameObject.transform.position), Quaternion.identity);
GUIDamage.guiText = Fire.MissileDamage;
}
}
i would create a gui text (or gui texture, but i think its easier with text) prefab, and , if the enemy received damage, it instantiate this prefab, like that:
and add a Destroy(gameObject,time) in a script for the prefab.
by the way, how exactly you are sotring this variable? depending on how you are doing that, i would suggest a way to put the correct hits number in the text (basically, guiText.text = (hits))
well, i tryed implementing the code in a space invaders like tutorial game i made, and it worked like that:
-i created a "hits" prefab with a GUIText attached to it
-hits has a script with a Destroy(gameObject,1.5); function in the update() override
-added a hitPoints variable to the enemy prefab
-overrider ontriggerenter to check if it colided with a "player_shot", and if thats the case (and hitPoints > 1, otherwise it destroys the enemy), it executes
hitPoints--;
var hits_ : GameObject = Instantiate(hits,Camera.main.WorldToViewportPoint(gameObject.transform.position),Quaternion.identity);
hits_.guiText.text = ("+1");
since MissileDamage is a public static variable (never tryed to use one but i belive u can use like that), you could cahnge ("+1") to (MissileDamage)
BUT... that code would create this damage message just on the enemy, and you want it a bit over it.
i think this could be done if you used
as the vector3 position for the instantiate (since guitext use viewport cordinate, you have to convert the point to that and i beelive this is the best way. you can also try adding the y of the worldtoviewportpoint, but remember that visible values range from 0 to 1)
if you dont want it to follow the enemy, then it is quite easy (but im not sure if it would be more perfomance friendly if it actually followed the enemy), add a private vector 3 variable to store the position of the guitext when it was created (i called it pos0). in Start (), add this
which will make sure that the guitext will be in the same position of the world (but going up a bit), no matter where the camera is looking at
im not sure how good this really looks since i just addad a simple smoothlookat script to the camera, and in my case it screws the gameplay up
and if you think that its not looking right, you can try adding a enemy_ GameObject variable to the hits script, and when you instantiate it, you make something like
and instead of positioning the guitext according to the start position, you would position it according to the enemy position (and make a counter to add to enemy y position at every update)