Dynamic Spline Creation - Sprint 7
Author: Tyler Sessler
The goal was simple- after a charge attack, I wanted my enemy to loop back towards the player on a smooth curve. There are a few requirements to this task:
1. The enemy should end up facing the player's location.
2. The enemy should feel natural and move without sharp adjustments.
3. If the player is moving, the enemy should still be able to find the player.
If we start with problem one, this gives us a few starting points. The enemy must know the player's location, the way it's currently facing, and the distance between them. To create pathfinding logic that follows these ideas, it must change in size and shape depending on the situation. If the player is further away, the angle can be larger. Splines are a great example of curved, adjustable shapes, so this is the method of choice.
Splines are often static, predetermined paths typically placed in the level manually. This is not an option for us as the enemy's position will be largely varied. Every time we make a spline, it needs to be suited for the task. I started with attaching a spline component to my enemy. This creates a scenario where the spline will always be positioned around the enemy, and will never have to be pre-placed. While a good starting point, this posed a few problems (see below). When my enemy rotated to follow the spline, the spline rotated with it. This created a situation mimicking a dog chasing its own tail.
To fix this, I created a custom SplineActor class. Inside, logic is created to form the intended curve based on values we pass in (StartPoint, EndPoint, TurnRadius). To get this effect applied to our enemy, we still need a SplineComponent. This time, however, the curvature logic is handled inside our SplineActor class. I attached the component to my enemy, creating separate, world-based logic. Therefore preventing the tail-chasing effect.
After fine tuning the curve, I started having my enemy follow the path. Initially, this was done with a timeline and curve asset. I used SetActorLocations each tick, following the spline based off of the timeline and curve created prior. A few issues with this. First, each path, regardless of size, would utilize the same time-flow. If I had to travel 300 units or 3000, it would still take the same 5 seconds. Secondly, using SetActorLocation teleports the enemy inch by inch. Not only does this not flow logically, but it prevents the enemy from having any actual control. It slides across the ground and uses the idle animation as the speed was set to 0. Rotations were set very similarly. This created an effect similar to a roller-coaster on the tracks.
Eventually, I ended up with a waypoint system. As the spline is drawn, every 'X' units a waypoint is created. This variable can be increased or decreased depending on your situation. I check the waypoint progress and save it as a percentage (utilized for checks later on), the direction of movement, and the distance between points. After reaching a waypoint, the index is incremented and a new destination is set. In order to move, I used the CharacterMovement's AddInputVector function. Now my enemy properly walks along the spline.
Additionally, if the enemy is at least 25% through the spline, it begins to check if the player is seen. This is not done using the sight perception component, as I do not use any other perception systems throughout my game. Instead, I used a DotProduct function calculating between the enemy's forward vector and the direction to the player. Add in a DegreesToRadians, and now I am able to check a sight angle that best fits the situation. If the enemy does detect the player, the spline movement is finished early.
The last thing to address is the merging of the spline logic with my current behavior tree. Utilizing an Enum labeled as "States", I created a path only used during the retreating state. After charging past the player, my enemy begins the spline drawing and path following sequence. It is held in-progress until either of the exit conditions are met, then sets the state back to default. I also have a task to set the enemy's speed to a temporary value, allowing control over the retreat's timing.
Get Proving Grounds
Proving Grounds
A arena based roguelike horde survival themed around the divine
Status | In development |
Author | NullNotZero |
Tags | Bullet Hell, Hack and Slash, Isometric, Roguelite |
More posts
- Boss MechanicsJul 20, 2024
- Creating AI Concepts - Sprint 6Jul 20, 2024
- Projectile Offset Hit Detection - Sprint 5Jul 13, 2024
- Area of EffectsJul 13, 2024
- Better AimingJul 07, 2024
- Creating LevelsJun 28, 2024
- Animation and Valhalla LevelJun 28, 2024
- Buffs/Debuffs & DoTs/HoTsJun 28, 2024
- Retargeting for Animation - Sprint 3Jun 27, 2024
Leave a comment
Log in with itch.io to leave a comment.