Last active
October 27, 2023 21:55
-
-
Save sayhisam1/dd3e30f5e638a6ebdee1f2141df7ebc3 to your computer and use it in GitHub Desktop.
Matter useAnimation hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local ReplicatedStorage = game:GetService("ReplicatedStorage") | |
local Matter = require(ReplicatedStorage.Packages.Matter) | |
local function useAnimation(animator: Animator, animation: Animation, pause: boolean) | |
if pause == nil then | |
pause = false | |
end | |
local state = Matter.useHookState(animator, function(storage) | |
if storage.animationTrack then | |
storage.animationTrack:Stop() | |
storage.animationTrack = nil | |
storage.animation = nil | |
end | |
end) | |
if state.animation ~= animation then | |
state.animation = animation | |
if state.animationTrack then | |
state.animationTrack:Stop() | |
state.played = nil | |
state.animationTrack = nil | |
end | |
end | |
if state.animationTrack == nil then | |
state.animationTrack = animator:LoadAnimation(animation) | |
end | |
local looped = state.animationTrack.Looped | |
if not state.animationTrack.IsPlaying and (looped or (not looped and not state.played)) then | |
state.played = true | |
state.animationTrack:Play() | |
end | |
-- pause or unpause the animation | |
state.animationTrack:AdjustSpeed(pause and 0 or 1) | |
return state.animationTrack | |
end | |
return useAnimation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment