Is it possible to bind an event (script function call) to a specific frame in the animation? Or how to catch this frame? (state change is not suitable)
1 answer
Easy.
In animation, you can do a lot of things, including setting various events.
For example, the script has a method:
public void PrintFloat(float someValue) { Debug.Log("Метод PrintFloat печатает значение: " + someValue); } In the Animation window, you can add an animation event to the clip at the current playback position by clicking the Event button or at any point in the animation by double-clicking on the Event Line at the point where you want to add an event trigger:
When you add an event, a dialog box appears to indicate the name of the function and the value of the parameter you want to pass to it:
Events added to a clip in the event string are shown as markers. Holding the mouse over the marker shows a hint with the name of the function and the value of the parameter.
Happy end!
Now about another item from the question (or the second method):
Or how to catch this frame?
In theory, you can find out how many frames will go clip. The formula is as follows:
int totalFrames = (int)Mathf.Abs(Mathf.Floor((fps * animationClip.length) / animationSpeed)); In this formula, the variables:
- fps - the number of frames to be replaced per unit of time. In theory, you can put a constant value equal to 60. But this is not exactly
- animationClip.length - the length of the clip in seconds, where
animationClipis of type AnimationClip - animationSpeed - animation speed. A value of type
float. Animation in the normal state is set to 1. More value - more speed.
Further, knowing that the Update method is called 60 frames per second (again in theory), then you can put a counter that counts from 1 to totalFrames animation frames and at the right time (for example, if the counter is between 5 and 6 animation frames) pull the trigger ( method) for any script of any object.




Или как отловить этот кадр..... maybe in the future it will also be useful - Alexey Shimansky