How would I add a jetpack to my script?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By PBR00

I am trying to get a jetpack hover working similar to mario sunshine’s hover ability. I can’t seem to get it to keep hover after holding down the button for six seconds. Any advice is very much appreciated.

Here is my move script:
extends PlayerState

export var max_speed: = 25.0
export var move_speed: = 25.0
export var gravity: = -80.0
export var jump_impulse: = 25.0
export var attack: = 10.0
export var hover_impulse: = 10.0
var velocity: = Vector3.ZERO

func unhandled_input(event: InputEvent) → void:
if event.is_action_pressed(“jump”):
_state_machine.transition_to(“Move/Air”, { velocity = velocity, jump_impulse = jump_impulse })
elif event.is_action_pressed(“attack”):
_state_machine.transition_to(“Move/Punch”, { attack = attack})
if event.is_action_pressed(“jet”):
_state_machine.transition_to(“Move/Hover”, { velocity = velocity, hover_impulse = hover_impulse })

func physics_process(delta: float) → void:
var input_direction: = get_input_direction()

var move_direction: = input_direction
if move_direction.length() > 1.0:
	move_direction = move_direction.normalized()
move_direction.y = 0.0

if move_direction:
	player.look_at(player.global_transform.origin + move_direction, Vector3.UP)

velocity = calculate_velocity(velocity, move_direction, delta)
velocity = player.move_and_slide(velocity, Vector3.UP)

static func get_input_direction() → Vector3:
return Vector3(
Input.get_action_strength(“move_right”) - Input.get_action_strength(“move_left”),
Input.get_action_strength(“move_back”) - Input.get_action_strength(“move_front”)


func calculate_velocity(
velocity_current: Vector3,
move_direction: Vector3,
delta: float
) → Vector3:
var velocity_new := move_direction * move_speed
if velocity_new.length() > max_speed:
velocity_new = velocity_new.normalized() * max_speed
velocity_new.y = velocity_current.y + gravity * delta

	return velocity_new

And here is the hover script:

extends PlayerState

func physics_process(delta: float) → void:

if player.is_on_floor():

func enter(msg: Dictionary = {}) → void:
match msg:
{“velocity”: var v, “hover_impulse”: var hi}:
_parent.velocity = v + Vector3(0.0, hi, 0.0)

func exit() → void:

Code is quite hard to read (formatting issue).

I noticed you are using a state machine. It looks like 3d Mannequiny by GDQuest.

I would start by making a copy of Jump state and then removing the gravity logic, I don’t remember if that was in the base Move player state, you could always generate a new base state without inheriting from Move.

And you could split in two different states: one for the “jump” part where you reach the max elevation then after few seconds transition to the actual hover part where you don’t fall.

Then you could transition to Jump/Fall state when you finish water or whatever form of energy you use for your jetpack.

gioele | 2021-07-27 23:30

Cool. Thanks for the advice.

PBR00 | 2021-10-26 00:52