A few days ago in the development of the project encountered a such demand, RPG games, special effects and animation playback is not synchronized. If the protagonist attacks the NPC, first instantiate the effect, then play the animation. After all, the animation has a length of time. When the animation plays the attack brandished the moment, the effect may have already played out. So just think of the animation to a certain frame of time to instantiate the effect, so that the animation and effects are not synchronized with the problem. The first thing we want to do is how to add an event to an animation?
1, create a blank Unity project and import an animated model.
There's basically so much to prepare for, and then you can add animated events. There are two ways to add animated events: Method One: Add events directly to the model
The setting here is complete. Run down to see what happens.
Error, the return function can not find the receiver, it is plain that we have not given back the definition of the function, then in the script to define the return function!
OK, mount the script in the scene and run again.
The animation plays, and the back function also takes effect! At this point the instance effect is OK.
Then the second way, we can add arbitrary events to any frame of any animation, seemingly a bit capricious. It seems to be the same as the real one, so let's see if it's true.
Before code code, we first learn a thing called animationevent.
Knowing these parameters, we can write the code.
usingUnityengine;usingSystem.Collections; Public classtestanimationevent:monobehaviour{/// <summary> ///we want to add an animation of the event/// </summary> PublicAnimationclip clip; Publicgameobject Target; //Use this for initialization voidStart () {addanimationevent (); } /// <summary> ///custom events in code/// </summary> Public voidaddanimationevent () {//Create an animated eventAnimationevent animationevent =Newanimationevent (); //Set event fallback function nameAnimationevent.functionname ="Gototarget"; //Incoming ParametersAnimationevent.objectreferenceparameter =Target; //Setting the trigger frameAnimationevent.time =1.0f; //Registering Eventsclip. Addevent (animationevent); } /// <summary> ///visualize adding animated frame events/// </summary> Public voidcallfuncation () {Debug.Log ("Animation Event triggered!"); } /// <summary> ///code to add a back-off function/// </summary> /// <param name= "Go" ></param> Public voidgototarget (Gameobject go) { This. Transform. LookAt (Go.transform); Debug.Log ("target object:"+ Go.name +" "+go.transform.position); if(Vector3.distance ( This. Transform.position, Go.transform.position) <0.2f) { return; } Else{transform.position= Vector3.movetowards ( This. Transform.position, go.transform.position, 2f); } }}
OK, let's run it, the test result is that the character moves to the target point.
After running
Well, to this basically completed, if there is any doubt, contact me Oh!
Project code: Http://pan.baidu.com/s/1fZ9pW
Unity 3D Animated Frame Event