Let's enhance the rail grinding system to enable moving backward along the spline and also ensure the character jumps or exits automatically at the end of the spline. Here’s a detailed guide on how to achieve this:
Step-by-Step Implementation
-
Create the Rail Spline: As previously detailed.
-
Set Up Character Blueprint: As previously detailed with additional controls for moving backward.
Step 1: Create the Rail Spline
- Create BP_Rail: As previously detailed.
- Add Spline Component: As previously detailed.
Step 2: Set Up Character Blueprint
- Add Detection Sphere: As previously detailed.
- Overlap Event Setup: As previously detailed.
- Setup Variables: Add necessary variables for controlling movement direction and speed.
New Variables to Add:
MoveDirection (float): 1 for forward, -1 for backward.
BackwardSpeed (float): Speed for moving backward, default can be e.g., 300.0.
Updated Blueprint Setup Steps
OnComponentBeginOverlap Event
Ensure the event sets GrindingDistance and other variables correctly.
MoveAlongRail Function
Enhanced function to handle both forward and backward movement, and automatic jump or exit at the end or start of the spline.
Step 3: Enter Rail Logic
OnComponentBeginOverlap Event
OnComponentBeginOverlap (RailDetectionSphere)
├── Cast to BP_Rail (Other Actor)
| └── As BP_Rail
| ├── IsValid(CurrentRail)
| | └── Branch
| | ├── True: Do Nothing
| | └── False: [Continue]
| ├── Set CurrentRail
| ├── Set bIsGrinding = True
| ├── Get Actor Location → ActorLocation
| ├── CurrentRail → RailSpline → FindInputKeyClosestToWorldLocation (ActorLocation) → ClosestKey
| ├── RailSpline → GetDistanceAlongSplineAtSplineInputKey(ClosestKey) → ClosestDistance
| └── Set GrindingDistance = ClosestDistance
Step 4: Rail Movement and Exit Logic
MoveAlongRail Function:
Add additional logic to handle both forward and backward movement, and auto exit when reaching either end.
Function MoveAlongRail:
MoveAlongRail (Float DeltaTime)
├── Branch [bIsGrinding]
| └── True:
| ├── Add [GrindingDistance + (MoveDirection * GrindingSpeed * DeltaTime)]
| ├── Get CurrentRail → RailSpline
| ├── RailSpline → GetSplineLength → SplineLength
| ├── Branch [GrindingDistance >= SplineLength OR GrindingDistance <= 0]
| | └── True:
| | ├── Set bIsGrinding = False
| | ├── Launch Character [Launch Velocity -> (0, 0, 1000)]
| └── False:
| ├── GetLocationAtDistanceAlongSpline (GrindingDistance, World) → NewLocation
| ├── GetRotationAtDistanceAlongSpline (GrindingDistance, World) → NewRotation
| ├── Set Actor Location [NewLocation]
| └── Set Actor Rotation [NewRotation]
Implementation Details:
-
Branch Node for bIsGrinding:
- Check if
bIsGrinding is True.
-
Add Node for Grinding Distance:
- Increment or decrement
GrindingDistance based on MoveDirection and GrindingSpeed.
-
Get Spline Length:
- Get the spline length from
CurrentRail.
-
Branch Node to Check if End or Start of Spline is Reached:
- Compare
GrindingDistance with spline length and 0.
-
Handle Exit on Reaching End or Start:
- If
True, set bIsGrinding to False and handle exiting or jumping.
- Else, proceed with updating actor location and rotation.
Nodes Setup in MoveAlongRail:
Full Node Setup in Blueprint
MoveAlongRail (Float DeltaTime)
├── Branch [bIsGrinding]
| └── True:
| ├── Add [GrindingDistance + (MoveDirection * GrindingSpeed * DeltaTime)]
| ├── Get CurrentRail → RailSpline
| ├── RailSpline → GetSplineLength → SplineLength
| ├── Branch [GrindingDistance >= SplineLength OR GrindingDistance <= 0]
| | └── True:
| | ├── Set bIsGrinding = False
| | ├── Launch Character [Launch Velocity -> (0, 0, 1000)]
| └── False:
| ├── GetLocationAtDistanceAlongSpline (GrindingDistance, World) → NewLocation
| ├── GetRotationAtDistanceAlongSpline (GrindingDistance, World) → NewRotation
| ├── Set Actor Location [NewLocation]
| └── Set Actor Rotation [NewRotation]
Event Tick
As previously detailed,
Event Tick
├── MoveAlongRail [DeltaTime]
Input Action Jump
As previously detailed:
Input Action Jump
├── Branch [bIsGrinding]
| └── True:
| ├── Set bIsGrinding = False
| └── [Launch Character]
Control for Moving Backwards
Add Input for Switching Direction
-
Create Input Action for Switching Direction (Backward/Forward):
- Go to Edit → Project Settings → Input.
- Add a new Action Mapping, e.g., "SwitchDirection" and assign a key (e.g., "S" for moving backward, "W" for moving forward).
-
Handle Input Action in Character Blueprint:
- In the Event Graph of
BP_Character, add logic to switch MoveDirection.
Blueprint Event Setup:
InputAction SwitchDirection
├── Branch [bIsGrinding]
| └── True:
| ├── Switch MoveDirection
| ├── Case: "S" → Set MoveDirection = -1 (Backward)
| └── Case: "W" → Set MoveDirection = 1 (Forward)
Final Blueprint Nodes for Input Handling:
InputAction SwitchDirection
├── Branch [bIsGrinding]
| └── True:
| └── Select Float:
| ├── Set MoveDirection = -1 (Backward)
| └── Set MoveDirection = 1 (Forward)
Testing
- Ensure Backward and Forward Movement along the spline is smooth and correct.
- Verify Automatic Jump/Exit when reaching either end of the spline.
- Check Input Handling for Switching Direction (Forward/Backward).
By following these steps, your character should be able to move both forwards and backwards along the spline, and automatically jump or exit when reaching the end or start of the spline.