Godot Version
4.2.2.stable.official.15073afe3
Question
Hey everyone, here’s some context on the issue I’m running into:
I want to have my Player (a CharacterBody2D) follow a dynamically generated Path2D (which is generated on a specific input) to mimic a curve-shaped “dash”. Here’s the general approach I’m thinking of to accomplish this:
Here’s the current tree structure of the project:
- Main
- Player
- bunch of textures
- bunch of terrain
All of the script logic is in Player.gd (attached to Player node)
When “dash” action is triggered, do a couple of set-up steps:
make a new Path2D node and assign it as child of the root level
make a PathFollow2D and attach it as a child of the newly created Path2D node
make a new Curve2D and assign it 4 new points (these will be the 4 points of a cubic bezier curve)
assign the new Curve2D to the “curve” field of the newly created Path2D node
make the Player a child of the newly created PathFollow2D node
Based on the above steps, I think the new tree should look like this:
- Main
- bunch of textures
- bunch of terrain
- Path2D
- PathFollow2D
- Player
After the set-up steps are done, in physics_process we do the following:
- Player
- PathFollow2D
increment the progress of PathFollow2D by delta
assign a progress to a variable “t” (used as the parameter for bezier curve calculations)
get Player’s current position on the curve using t
get Player’s next expected position on the curve using “t + increment”
use the two above values to calculate the direction vector from the current position to the next expected position
update Player’s global “velocity” based on the direction vector scaled by a constant
When t reaches 1 (i.e. the character has finished moving along the entire curve), we do the following:
move the Player back to where it started from, and delete the Path2D (and all its children), so the tree looks like this again:
-Main
- Player
- bunch of textures
- bunch of terrain
Couple of questions regarding all of the above:
Is this generally the right approach to have a character follow a dynamically created path?
Does the bezier curve logic seem generally right?
Does the approach of temporarily moving Player under the dynamically generated PathFollow2D and then moving it back under main make sense?
- If the approach does make sense, how do we specifically accomplish this using the remove_child, add_child, reparent, get_parent, etc. functions???
Would really appreciate some help here, I’ll answer any follow ups ASAP!