- Home /
How do i make my script count once an animation on another script has finished?
Hey guys,
so, im continuing making a little quest tracking system. I want it so when a enemy wolf dies or when i click B the counter goes up. I've set it up in code, however it counts +1 for when the animation is present so it counts to 10 automatically.
Here's the code for the quest tracker:
using UnityEngine;
using System.Collections;
public class Quest1tracker : MonoBehaviour {
public AudioClip Questcompletesound;
public Transform NPC;
public Transform Wolf;
int counter = 0;
int Maxcount = 10;
void OnGUI(){
GUI.Box( new Rect((Screen.width /2) +800,400,150,25), "Wolfs Killed " + counter + "/" + Maxcount);
}
void Update(){
WolfHealth Eh = (WolfHealth)Wolf.GetComponent("WolfHealth");
if (Input.GetKeyDown(KeyCode.B) || Eh.Deathanimation) {
counter++;
}
if(counter == Maxcount) {
audio.clip = Questcompletesound;
audio.Play();
NPC.GetComponent<Quest1Middle>().enabled = false;
NPC.GetComponent<Quest1End>().enabled = true;
GetComponent<Quest1Handintracker>().enabled = true;
GetComponent<Quest1tracker>().enabled = false;
}
}
}
And this is the wolf health script(which contains death and death animation):
using UnityEngine;
using System.Collections;
public class WolfHealth : MonoBehaviour {
public int MaxHealth = 320;
public int CurHealth = 320;
public int Death = 0;
public GUIStyle Playercurhealthtext;
public float HealthBarLength;
public Transform Target;
public GUIStyle Demonskin;
public AnimationClip Deathanimation;
void Start(){
HealthBarLength = 200;
}
void OnGUI () {
float dist = Vector3.Distance(Target.position, transform.position);
if( dist <15){
GetComponent<EnemyAI>().enabled = true;
GUI.Box(new Rect((Screen.width /2)+310,(Screen.height /2) +300,90,80),"", Demonskin);
GUI.Box(new Rect((Screen.width /2) +110,(Screen.height /2) +300,200/(MaxHealth/CurHealth),20), CurHealth + "/" + MaxHealth, Playercurhealthtext);
}
if(dist > 20) {
GetComponent<EnemyAI>().enabled = false;
}
}
public void AdjustCurrentHealth(int Adj){
CurHealth += Adj;
if (CurHealth <0)
CurHealth = 0;
if(CurHealth > MaxHealth)
CurHealth = MaxHealth;
if(MaxHealth < 1)
MaxHealth = 1;
if(CurHealth == MaxHealth){
animation.AddClip(Deathanimation, "Death");
animation.Play();
GetComponent<Deactivateparent>().enabled = true;
GetComponent<EnemyAI>().enabled = false;
GetComponent<WolfHealth>().enabled = false;
}
HealthBarLength = (200) * (CurHealth/(float)MaxHealth);
}
public void update() {
AdjustCurrentHealth(0);
}
}
So how might i be able to get the counter to go up at the end of death animation. Also, can it be done so it counts 1 per death animation as I'd like to have multiple targets.
Many thanks in advance.
Answer by Jeffom · Jan 04, 2013 at 05:32 PM
You can do something like this
if(CurHealth == MaxHealth){
KillWolf();
}
*
*
*
private void KillWolf()
{
animation.AddClip(Deathanimation, "Death");
animation.Play();
GetComponent<Deactivateparent>().enabled = true;
GetComponent<EnemyAI>().enabled = false;
GetComponent<WolfHealth>().enabled = false;
Invoke("UpdateQuest",animation.clip.lenght );
}
private void UpdateQuest()
{
//call the update counter here
}
The UpdateQuest will called after the death animation. You should create some static manager that contains the questtrackers and then update the quest tracker from there
Hope this was helpful
Doesn't work. mono says Invoke(UpdateQuest,animation.length) The length doesn't exist I (corrected the grammar mistake on the post for lenght). Also i get this error CS1502: The best overloaded method match for `UnityEngine.$$anonymous$$onoBehaviour.Invoke(string, float)' has some invalid arguments
I did that it still have this error error CS1502: The best overloaded method match for `UnityEngine.$$anonymous$$onoBehaviour.Invoke(string, float)' has some invalid arguments Also ins$$anonymous$$d of the counter being called in the health bar script. How do i make it so the value goes up on the separate quest tracker script?
use this Invoke("UpdateQuest",animation.clip.lenght ); my bad, forgot to add the string format
you should have a main tracker with your quest being tracked, if you don't know what i'm talking about searching for singletons and static variables, that should solve youre problem
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How do i make a quest in c# for my game? 1 Answer
Quests/Dialogue (C#) 0 Answers