I have an animation for my game that has a initial starting part and then the last 2 or so seconds are made to be loop able. Does anyone know of a way to specify that I only want to loop that part instead of the entire animation?
Please tell me if I’m dreaming of imaginary functionality.
What exactly is the animation about?
Im not sure if this is the most efficient way to achieve what youre trying to go for, but I suggest taking the keyframes of the last 2 second of the animation that you want to use and then separate it into 2 animations you can play in line with eachother, that way animation 1 doesn’t loop but you can set animation 2 to loop.
It’s a death animation. The first part is the character falling over and the other part is an idle part in the fallen position. I thought of trying the two-animation thing, but I was hoping to be able to just use one.
It looks like there is a way to do this with one animation. I am working on understanding it and then I will provide you a response and how to do it. On the other hand if you’d just like to learn it yourself,
Here are all the properties you can use in an animation.
Under here you will find a property called TimePosition. I believe if you go under your animation and create an animation event, and drag it to the point in which you want to loop, you can use an if statement to check when the animation reaches the animation event and if so to set the animation to looped with TimePosition in place to loop only where the event is.
This is speculation for now so I can’t necessarily count on this working for you but I will be trying it to see if I can help.
You can detect when the animation track reaches a specific keyframe using KeyframeReached
(seems it’s deprecated)
and rewind the track to the position that u want to start the loop from by setting the property .TimePosition
Note: you need to rename keyframes from the animation editor.
This might not be the best way to do this, but by doing this way you will only need 1 animation.
By utilizing animation events and setting the timeposition of the animation, you can accomplish creating a loop at the timeframe of your choice in the animation.
Step 1
Create 1 animation event where you want the loop to end
Step 2
Once the loop end event fires, you can set the .TimePosition of the animation to where you want the animation to repeat again
So this will create a loop where everytime it fires the event end it repeates over and over again.
Example
--Set this variable to where you want the loop to start. Tweak the value from 1-100 until its good
--Remember to set a animation event called endOfLoop where you want the loop to end
local percentagePosition = 50 -- 1 - 100
deathAnimation:GetMarkerReachedSignal("endOfLoop"):Connect(function(paramString)
animationTrack.TimePosition = (percentagePosition / 100) * animationTrack.Length
end)
Okay so after messing with it for a bit this is how I managed to recreate it. Someone will probably find a simpler way to do it but here is how I did
I used a key to play the animation so yours will differ a little.
Refer to the second function and then work back up.
--Stuff
local UIS = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local chr = plr.Character
local hum = chr:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")
--Animation
local testan = animator:LoadAnimation(script:WaitForChild("Animation"))
--This function repeats your animation.
local function rewind(testan, TimePosition)
local dead = true
while dead == true do
testan.TimePosition = TimePosition
testan:AdjustSpeed(1)
task.wait() --However long you want to play the animation before it repeats itself.
end
end
UIS.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.H then --If the player presses H then it will play the animation
testan:play()
testan:GetMarkerReachedSignal("loop"):Connect(function() -- This function is for when the animation track reaches the animation event you created in the editor.
testan:AdjustSpeed(0) -- This essentially stops the track without disabling it(Necessary for timePosition to work)
rewind(testan, 0)-- This is where you call your function, with the second parameter being where the animation track will start its loop. You'll change that number to fit your animation.
end)
end
end)
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.