How To Animate Your Character In Roblox Studio
The 2d way of using animations is to play them in response to a character'south action in-game: for instance, if a player picks up an item, or takes impairment.
In this next script, whenever a player presses a button, a shock animation will play and paralyze them until the animation finishes.
Setup
The remainder of this course uses a pre-made model that includes a ProxmityPrompt. Players tin walk up to a push and printing information technology to activate an event.
-
Download the Shock Push button model and insert it into Studio.
Models tin can be added into your Inventory to be used in any game.
-
In a browser, open up the model page, click the Get push. This adds the model into your inventory.
-
In Studio, get to the View tab and click on the Toolbox.
-
In the Toolbox window, click on the Inventory button. And so, make sure the dropdown is on My Models.
-
Select the Daze Button model to add it into the game.
-
-
In StarterPlayer > StarterPlayerScripts, create a local script named PlayShockAnimation.
-
The code below calls a function named
onShockTriggerwhen the proximity prompt is activated. Copy it into your script.local Players = game:GetService("Players") local player = Players.LocalPlayer local graphic symbol = player.Character if not character or not graphic symbol.Parent so character = player.CharacterAdded:Look() end local humanoid = character:WaitForChild("Humanoid") local Animator = humanoid:WaitForChild("Animator") local shockButton = workspace.ShockButton.Push local proximityPrompt = shockButton.ProximityPrompt local shockParticle = shockButton.ExplosionParticle local function onShockTrigger(player) shockParticle:Emit(100) end proximityPrompt.Triggered:Connect(onShockTrigger)
Create and Load an Animation
Animations that the player uses are stored on the player's Animator object. To play the shock blitheness, a new blitheness runway will need to exist loaded onto the Animator object when they join the game.
-
To a higher place
onShockTrigger, create a new Blitheness instance namedshockAnimation. Then, ready theAnimationIDof that to the desired animation. Use the ID in the code box if needed.local shockButton = game.Workspace.ShockButton.Button local proximityPrompt = shockButton.ProximityPrompt local shockParticle = shockButton.ExplosionParticle local shockAnimation = Instance.new("Animation") shockAnimation.AnimationId = "rbxassetid://3716468774" local function onShockTrigger(actor) end
-
Create a new variable named
shockAnimationTrack. On the actor's Animator, callLoadAnimation, passing in the previously created blitheness.local shockAnimation = Instance.new("Animation") shockAnimation.AnimationId = "rbxassetid://3716468774" local shockAnimationTrack = Animator:LoadAnimation(shockAnimation) -
With the new animation loaded, change some of the track's properties.
- Fix the AnimationPriority to
Activeness- Ensures the animation overrides any current animations playing. - Set Looped to
falseand so the animation doesn't repeat.
local shockAnimation = Instance.new("Animation") shockAnimation.AnimationId = "rbxassetid://3716468774" local shockAnimationTrack = Animator:LoadAnimation(shockAnimation) shockAnimationTrack.Priority = Enum.AnimationPriority.Activeness shockAnimationTrack.Looped = false - Fix the AnimationPriority to
Play the Animation
Whenever someone triggers the ProximityPrompt on the button, information technology'll play an blitheness and temporarily freeze that histrion.
-
Find the
onShockTriggeroffice. On theshockAnimationTrack, phone call thePlayfunction.local function onShockTrigger(player) shockParticle:Emit(100) shockAnimationTrack:Play() cease -
To prevent the player from moving while the blitheness plays, change the humanoid's
WalkSpeedproperty to 0.local function onShockTrigger(player) shockParticle:Emit(100) shockAnimationTrack:Play() humanoid.WalkSpeed = 0 stop
Using Animations with Events
Just how parts accept Touched events, animations accept events such every bit AnimationTrack.Stopped. For the script, once the animation finishes, you lot'll restore the thespian's motion speed.
-
Access the
Stoppedevent for the animation track using the dot operator, then call theExpectoffice. This pauses the code until that animation finishes.local part onShockTrigger(histrion) shockParticle:Emit(100) shockAnimationTrack:Play() humanoid.WalkSpeed = 0 shockAnimationTrack.Stopped:Expect() end -
Return the histrion'south walk speed to 16, the default for Roblox players.
local function onShockTrigger(role player) shockParticle:Emit(100) shockAnimationTrack:Play() humanoid.WalkSpeed = 0 shockAnimationTrack.Stopped:Wait() humanoid.WalkSpeed = 16 end -
Exam the game past walking up the part and printing East to get a daze.
The framework in this script can be easily adapted to different gameplay situations. For instance, try playing a special blitheness whenever a thespian touches a trap function, or whenever a team wins a game circular.
Source: https://developer.roblox.com/en-us/onboarding/scripting-avatar-animations/2
Posted by: brooksimalk1993.blogspot.com

0 Response to "How To Animate Your Character In Roblox Studio"
Post a Comment