- Home /
Boolean doesn't change as I want
I have put boolean like this
function OnTriggerEnter(other : Collider){
if(other.tag == "shot"){
move = false;
hurt = true;
health--;
Destroy(other);
}
}
but it never change as I type in function update
function Update(){
if(move && health > 0 && !hurt){
GetComponent(NavMeshAgent).destination = target.position;
animation.Play("walk01");
}
if(hurt && health > 0){
animation.Play("damage01");
hurt = false;
}
playerDistance = Vector3.Distance(player.transform.position, transform.position);
if(playerDistance <= 1){
if(!attacking && health > 0 && ! hurt){
move = false;
this.GetComponent(NavMeshAgent).Stop();
animation.Play("attack01");
Invoke("ApplyDamage", 1);
attacking = true;
}
}
if(playerDistance > 1 && playerDistance <= 10 && !attacking && health > 0 && !hurt){
move = true;
animation.Play("walk01");
}
if(playerDistance > 10 && health > 0){
move = false;
GetComponent(NavMeshAgent).Stop();
animation.Play("idle");
}
if(health <= 0){
die();
}
}
I want hurt become true so the animation damage can run.
Have you tried putting Debug.Log statements? Is the code inside the if statement in the OnTriggerEnter ever executed?
no, but the OnTriggerEnter can reduce health succesfully, so I think I can set boolean.
Answer by N1warhead · Oct 31, 2014 at 03:14 AM
I'm used to C#, but I'll try to point out what MAY be wrong.
Change this
function OnTriggerEnter(other : Collider){
if(other.tag == "shot"){
move = false;
hurt = true;
health--;
Destroy(other);
}
}
to this
function OnTriggerEnter(other : Collider){
if(other.gameObject.tag == "shot"){
move = false;
hurt = true;
health--;
Destroy(other);
}
}
Then the last part, I'd suggest restructuring your code so certain things are called before others. I've had that problem before with a bool where one was being called before the other just out of co-incidence.
Hope that helps!
I have fixed it
if(hurt){
if(health > 0){
move = false;
animation.Play("damage01");
}
hurt = false;
}
Animation can run when I don't use hurt = false. But I need to set it become false, so it will not loop.
Answer by smoggach · Oct 30, 2014 at 02:25 PM
You will not get the OnTriggerEnter callback unless one or more of the objects have a rigidbody. I'm guessing neither of your objects has one.
Answer by Unitraxx · Oct 30, 2014 at 02:34 PM
This is a duplicate question. You've asked exactly the same in http://answers.unity3d.com/questions/819747/how-can-i-play-damage-animation-after-colliding.html. Can someone please close this (or the other one)?
EDIT: Since you have 4 questions regarding the same piece of code, I suggest using the edit button to update your question instead of making new questions.
Thank you for your suggest, but I'm affraid they will say out of topic.
Your answer
Follow this Question
Related Questions
Damage Animation doesn't play 1 Answer
Animation not being played! 0 Answers