in unreal 5 blueprints I have made an event that when you press a button time slows down…
- User
in unreal 5 blueprints I have made an event that when you press a button time slows down using time dilation and if you press it again time would resume to normal speed now i want to add a duration to it so that after 2 seconds time goes to normal speed automatically and something like a power bar fills up when you are not using slow time and it goes down again in that 2 seconds when you are using it
- Assistant
Sure! In Unreal Engine 5 Blueprints, you can achieve this by using timers and variables to keep track of the power bar and the time-based slowdown. Here's a step-by-step approach:
-
Setup Variables:
bIsTimeSlowed: Boolean to check whether time is currently slowed.SlowTimeDuration: Float to store the duration of the slow time (e.g., 2 seconds).PowerBarValue: Float to store the power bar's current value.MaxPowerBarValue: Float to store the maximum value of the power bar (e.g., 100).PowerBarRechargeRate: Float to determine how fast the power bar refills.
-
Button Press Logic:
- On button press, check if
bIsTimeSlowedis false.- If false, activate slow time by setting time dilation, setting
bIsTimeSlowedto true, and starting a timer for the duration. - If true, set time dilation back to normal and set
bIsTimeSlowedto false.
- If false, activate slow time by setting time dilation, setting
- On button press, check if
-
Timer Logic:
- When the timer expires (after 2 seconds), set time dilation back to normal and set
bIsTimeSlowedto false.
- When the timer expires (after 2 seconds), set time dilation back to normal and set
-
Power Bar Logic:
- When time is slowed (
bIsTimeSlowedis true), decreasePowerBarValueover time. - When time is normal, increase
PowerBarValueover time until it reachesMaxPowerBarValue.
- When time is slowed (
Here's an example Blueprint logic:
Step 1: Create Variables
In your Blueprint (for example, in the Character Blueprint):
- Create the following variables:
bIsTimeSlowed(Boolean)SlowTimeDuration(Float, default 2.0)PowerBarValue(Float, default 100.0)MaxPowerBarValue(Float, default 100.0)PowerBarRechargeRate(Float, default 10.0)
Step 2: Button Press Logic
In the Event Graph of your Blueprint:
- Create an input event for the button press (e.g.,
InputAction SlowTime). - Add a branch node (
Ifstatement) to check thebIsTimeSlowedvariable. - If
bIsTimeSlowedis false:- Set Global Time Dilation (Set to a slower value, e.g., 0.2).
- Set
bIsTimeSlowedto true. - Set Timer by Function Name (Create a custom event to handle the timer's completion).
- If
bIsTimeSlowedis true:- Set Global Time Dilation to 1.0 (normal speed).
- Set
bIsTimeSlowedto false. - Clear the timer.
Step 3: Timer Logic
Create a custom event (e.g.,
OnTimeDilationEnd) to handle when the timer ends:- Reset Global Time Dilation to 1.0 (normal speed).
- Set
bIsTimeSlowedto false.
Step 4: Power Bar Logic
In the Event Graph:
- Use the
Event Ticknode to update the power bar value each frame. - Add a branch to check the value of
bIsTimeSlowed. - If
bIsTimeSlowedis true:- Decrease
PowerBarValuebased on the delta time and some depletion rate (e.g.,PowerBarValue -= DeltaTime * DepletionRate).
- Decrease
- If
bIsTimeSlowedis false:- Increase
PowerBarValuebased on the delta time andPowerBarRechargeRate(e.g.,PowerBarValue += DeltaTime * PowerBarRechargeRate). - Clamp
PowerBarValueso it doesn't exceedMaxPowerBarValue.
- Increase
Example Blueprint Nodes:
Event Tick |-> Branch (Condition: `bIsTimeSlowed`) |-> True: `PowerBarValue` -= `DeltaTime * DepletionRate` |-> False: `PowerBarValue` += `DeltaTime * PowerBarRechargeRate` |-> Clamp (`PowerBarValue`, 0, `MaxPowerBarValue`)Example Button Press Event:
InputAction SlowTime |-> Branch (Condition: `bIsTimeSlowed`) |-> True | |-> Set Global Time Dilation 1.0 | |-> Set `bIsTimeSlowed` False | |-> Clear Timer |-> False |-> Set Global Time Dilation 0.2 |-> Set `bIsTimeSlowed` True |-> Set Timer by Function Name (Function Name: OnTimeDilationEnd, Time: `SlowTimeDuration`)Example Timer Expiry Event:
Custom Event: OnTimeDilationEnd |-> Set Global Time Dilation 1.0 |-> Set `bIsTimeSlowed` FalseThis setup will ensure that the slow time mechanic works, automatically reverts after 2 seconds, and includes a power bar that depletes and refills accordingly.
-
- User
explain in detail how to implement this
- Assistant
Certainly! Below is a step-by-step guide to implementing the described behavior in Unreal Engine 5 using Blueprints.
Step 1: Create Variables
- Open your blueprint (e.g., Character Blueprint).
- In the Variables panel, click the "+" button to add new variables:
bIsTimeSlowed(Boolean, default: False)SlowTimeDuration(Float, default: 2.0)PowerBarValue(Float, default: 100.0)MaxPowerBarValue(Float, default: 100.0)PowerBarRechargeRate(Float, default: 10.0)DepletionRate(Float, default: 50.0) (to determine how fast the power bar depletes)
Step 2: Button Press Logic
- In the Event Graph, add an Input Action node for the button press (e.g.,
InputAction SlowTime). If you don't have an Input Action setup, you can set it in the Project Settings under Input. - Drag out a wire from the
Pressedpin of the Input Action node and add aBranchnode. - Drag the
bIsTimeSlowedvariable and hook it up to theConditioninput of the Branch node. - From the
Truebranch (i.e., ifbIsTimeSlowedis true):- Add a
Set Global Time Dilationnode and set itsTime Dilationto 1.0 (normal speed). - Add a
Setnode to setbIsTimeSlowedto false. - Add a
Clear Timernode to clear the timer.
- Add a
- From the
Falsebranch (i.e., ifbIsTimeSlowedis false):- Add a
Set Global Time Dilationnode and set itsTime Dilationto a slow value (e.g., 0.2). - Add a
Setnode to setbIsTimeSlowedto true. - Add a
Set Timer by Function Namenode:- Set the
Function Nameto the name of the custom event you're going to create (e.g., OnTimeDilationEnd). - Set the
Timeto the value of theSlowTimeDurationvariable. - Ensure
Loopingis unchecked.
- Set the
- Add a
Blueprint for Button Press Event:
InputAction SlowTime |-> Branch |-> Condition: bIsTimeSlowed |-> True | |-> Set Global Time Dilation to 1.0 | |-> Set bIsTimeSlowed to False | |-> Clear Timer |-> False |-> Set Global Time Dilation to 0.2 |-> Set bIsTimeSlowed to True |-> Set Timer by Function Name (Function Name: OnTimeDilationEnd, Time: SlowTimeDuration)Step 3: Timer Logic
- In the Event Graph, add a Custom Event node (e.g.,
OnTimeDilationEnd). - From the OnTimeDilationEnd node:
- Add a
Set Global Time Dilationnode and set itsTime Dilationto 1.0. - Add a
Setnode to setbIsTimeSlowedto false.
- Add a
Blueprint for Timer Expiry Event:
Custom Event: OnTimeDilationEnd |-> Set Global Time Dilation to 1.0 |-> Set bIsTimeSlowed to FalseStep 4: Power Bar Logic
- In the Event Graph, add an
Event Ticknode. - Drag out a wire from the
Tickpin and add aBranchnode. - Drag the
bIsTimeSlowedvariable and hook it up to theConditioninput of the Branch node. - From the
Truebranch:- Add a
Setnode to decreasePowerBarValueover time:PowerBarValue -= DeltaTime * DepletionRate(useFloat - FloatandFloat * Floatnodes to calculate this).
- Add a
- From the
Falsebranch:- Add a
Setnode to increasePowerBarValueover time:PowerBarValue += DeltaTime * PowerBarRechargeRate.
- Add a
Clampnode to ensurePowerBarValuedoes not exceedMaxPowerBarValue:Clamp (float)node withMin=0,Max=MaxPowerBarValue.
- Add a
Blueprint for Power Bar Logic (Tick Event):
Event Tick |-> Branch |-> Condition: bIsTimeSlowed |-> True | |-> Set PowerBarValue to PowerBarValue - (DeltaTime * DepletionRate) |-> False |-> Set PowerBarValue to PowerBarValue + (DeltaTime * PowerBarRechargeRate) |-> Clamp (PowerBarValue) (Min: 0, Max: MaxPowerBarValue)By following these steps, you should have a working setup in Unreal Engine 5 Blueprints where pressing a button slows down time for 2 seconds, and a power bar depletes while time is slowed and refills when it is not.
- User
what should i plug to the context pin of delta time
- Assistant
In an Unreal Engine blueprint, you don't directly plug anything into a "context" pin for
DeltaTime. Rather, you use a node calledGet World Delta Secondsto get the time elapsed between the current and previous tick, which is what you typically use to scale changes that are happening every frame.Here's a more detailed explanation and example of how to implement the
Event Ticklogic, including the use ofGet World Delta Seconds:Detailed Event Tick Logic
-
Add the Event Tick Node:
- In your blueprint's Event Graph, add an
Event Ticknode. This node executes every frame.
- In your blueprint's Event Graph, add an
-
Add the Branch Node:
- From the
Event Ticknode, drag out a wire and add aBranchnode. - Connect the
bIsTimeSlowedvariable to theConditionpin of theBranchnode.
- From the
-
Handling the True Branch (Time is Slowed):
- From the
Trueoutput pin of theBranchnode, drag out a wire and add aGet World Delta Secondsnode. This node will provide the time elapsed since the last frame, which allows us to scale our values correctly. - Drag out another wire from the
Truepin and add aSetnode to update thePowerBarValue. - Use
Float - FloatandFloat * Floatnodes to calculate the newPowerBarValueas follows:PowerBarValue - (DeltaTime * DepletionRate)
- From the
-
Handling the False Branch (Time is Normal):
- From the
Falseoutput pin of theBranchnode, drag out a wire and add aGet World Delta Secondsnode. - Drag out another wire from the
Falsepin and add aSetnode to update thePowerBarValue. - Use
Float + FloatandFloat * Floatnodes to calculate the newPowerBarValueas follows:PowerBarValue + (DeltaTime * PowerBarRechargeRate)
- Add a
Clamp (Float)node to ensure thePowerBarValuestays within specified bounds.- Set
Minto 0 andMaxtoMaxPowerBarValue.
- Set
- From the
Example Blueprint for Event Tick Logic
Event Tick |-> Branch |-> Condition: bIsTimeSlowed |-> True | |-> Get World Delta Seconds -> DeltaTime | |-> Set PowerBarValue | |-> PowerBarValue - (DeltaTime * DepletionRate) | |-> False | |-> Get World Delta Seconds -> DeltaTime | |-> Set PowerBarValue | |-> PowerBarValue + (DeltaTime * PowerBarRechargeRate) | |-> Clamp (Min: 0, Max: MaxPowerBarValue)Steps to Add and Connect Nodes:
-
Add Event Tick:
- Right-click in the Event Graph and search for
Event Tick.
- Right-click in the Event Graph and search for
-
Add Branch Node:
- Drag from the
Event Tickpin and search forBranch. - Connect the Boolean variable (
bIsTimeSlowed) to theConditionpin of the Branch.
- Drag from the
-
Add Get World Delta Seconds Nodes:
- Drag from the
TrueandFalsepins of the Branch separately, and search forGet World Delta Seconds.
- Drag from the
-
Add Set Nodes:
- Drag from the
TrueandFalsepins of the Branch, after theGet World Delta Secondsnodes, and search forSet. - Select
PowerBarValueas the variable to set.
- Drag from the
-
Add Arithmetic Nodes:
- To subtract in the True branch:
- Drag from the
DeltaTimeoutput pin ofGet World Delta Secondsand search for* (float). Set the other input pin toDepletionRate. - Drag from the result and search for
- (float). ConnectPowerBarValueto the top input.
- Drag from the
- To add in the False branch:
- Drag from the
DeltaTimeoutput pin ofGet World Delta Secondsand search for* (float). Set the other input pin toPowerBarRechargeRate. - Drag from the result and search for
+ (float). ConnectPowerBarValueto the top input.
- Drag from the
- To subtract in the True branch:
-
Add Clamp Node:
- For the False branch, drag from the resulting value (
PowerBarValue + (DeltaTime * PowerBarRechargeRate)) and search forClamp (float). - Set
Minto 0 andMaxto yourMaxPowerBarValue.
- For the False branch, drag from the resulting value (
By following these steps and connecting the nodes as described, you ensure that the
PowerBarValueis correctly updated each frame based on whether time is slowed down or normal. This setup will work smoothly within the game loop, accurately reflecting the player's usage and replenishment of the slow time ability. -