unity3d - Unity 3D: Transition of Animation -


i new unity3d , working on following tutorial:

https://www.youtube.com/watch?v=gxpi1czz5na

it worked fine.

i wanted add functionality if skeleton hits sword, real he's taking damage. sort of poorman's way of having sword collide objects.

but i've found doesn't work correctly. seem either have choice cause 'hit' put infinite loop, or ignore hit together. here code:

using system.collections; using system.collections.generic; using unityengine;  public class chase : monobehaviour {      public transform player;     static animator anim;      // use initialization     void start () {         anim = getcomponent<animator> ();     }      // update called once per frame     void update () {         //debug.log (anim.tostring ());         //debug.log ("start update");         vector3 direction = player.position - this.transform.position;         //debug.log ("distance: " + direction.magnitude.tostring ());         float angle = vector3.angle (direction, this.transform.forward);         //debug.log ("angle: " + angle.tostring ());         //debug.log(anim.getcurrentanimatorstateinfo(0).isname("damage"));          // top animation running         animatorstateinfo stateinfo = anim.getcurrentanimatorstateinfo(0);          if (vector3.distance (player.position, this.transform.position) < 10 && angle < 120 && !stateinfo.isname ("attack") && !anim.getbool("hit")) {              direction.y = 0;             this.transform.rotation = quaternion.slerp (this.transform.rotation, quaternion.lookrotation (direction), 0.1f);              anim.setbool ("isidle", false);             if (direction.magnitude > 2) {                 this.transform.translate (0, 0, 0.03f);                 anim.setbool ("iswalking", true);                 anim.setbool ("isattacking", false);                 anim.setbool ("hit", false);             } else {                 anim.setbool ("isattacking", true);                 anim.setbool ("iswalking", false);                 anim.setbool ("hit", false);             }         }         else{             anim.setbool ("isidle", true);             anim.setbool ("iswalking", false);             anim.setbool ("isattacking", false);             anim.setbool ("hit", false);         }     } } 

and here sword collision script:

using system.collections; using system.collections.generic; using unityengine;  public class swordcollision : monobehaviour {     private animator anim;      // collided object     private void ontriggerenter(collider collider) {         int layer = collider.gameobject.layer;         anim = this.getcomponentinparent<animator> ();         //debug.log (anim.tostring ());         if (layer != layermask.nametolayer ("floor") && layer != layermask.nametolayer ("monster")) {             debug.log ("sword hit something:" + collider.name.tostring());             debug.log (layermask.layertoname(layer));              anim.setbool ("isidle", false);             anim.setbool ("iswalking", false);             anim.setbool ("isattacking", false);             anim.setbool ("hit", true); // kicks off damage state         }     }  } 

i setup transitions attack , damage animations "has exit time" play way through. other transitions not have can interrupted right away upon 'hit' taking place.

the problem seems after sword collision script registers hit , sets "hit" boolean "true" (to kick off damage animation) chase script cancels it, hit never takes place. (i.e. anim.setbool ("hit", false);)

however, if comment out line, damage animation take place, gets stuck in loop because have nothing shut off.

this causing me pull hair out because attack animation setup identically. think reason 1 works correctly because boolean "isattacking" gets set 'true' continually in chase script until animation starts. because damage animation kicked off different script, there doesn't seem easy way me sure animation begins before allowing chase script change boolean value "hit".

is there way that? delay or guarantees animation changes states. or maybe way check when 'damage' animation completed before changing boolean value?

here solution came with. kinda hacky in opinion. still feel i'm not doing in best way.

using system.collections; using system.collections.generic; using unityengine;  public class chase : monobehaviour {      public transform player;     static animator anim;      // use initialization     void start () {         anim = getcomponent<animator> ();     }      // update called once per frame     void update () {         //debug.log (anim.tostring ());         //debug.log ("start update");         vector3 direction = player.position - this.transform.position;         //debug.log ("distance: " + direction.magnitude.tostring ());         float angle = vector3.angle (direction, this.transform.forward);         //debug.log ("angle: " + angle.tostring ());         //debug.log(anim.getcurrentanimatorstateinfo(0).isname("damage"));          // top animation running         animatorstateinfo stateinfo = anim.getcurrentanimatorstateinfo(0);          if (anim.getbool("hit") && !stateinfo.isname ("damage")) {             print ("keep going");             anim.setbool ("isidle", false);             anim.setbool ("iswalking", false);             anim.setbool ("isattacking", false);             anim.setbool ("hit", true);         }         else if (vector3.distance (player.position, this.transform.position) < 10 && angle < 120 && !stateinfo.isname ("attack") && !stateinfo.isname ("damage")) {              direction.y = 0;             this.transform.rotation = quaternion.slerp (this.transform.rotation, quaternion.lookrotation (direction), 0.1f);              anim.setbool ("isidle", false);             if (direction.magnitude > 2) {                 print ("following");                 //this.transform.translate (0, 0, 0.03f);                 anim.setbool ("iswalking", true);                 anim.setbool ("isattacking", false);                 anim.setbool ("hit", false);             } else {                 print ("attacking");                 anim.setbool ("isattacking", true);                 anim.setbool ("iswalking", false);                 anim.setbool ("hit", false);             }         }          else {             print("back idle");             anim.setbool ("isidle", true);             anim.setbool ("iswalking", false);             anim.setbool ("isattacking", false);             anim.setbool ("hit", false);         }     } } 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -