I want to make a flight mode for my character

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

Hello guys I am trying to figure out how I would be able to make a character “fly” so that in such where my mouse/camera view is pointed my player will fly that direction if I am pressing the “W” key. All I’ve been able to do so far is make sure gravity doesn’t affect my player while I press flight.

Here`s my script:

extends KinematicBody

var vertical_velocity = 0
var gravity = 50
var direction = Vector3.FORWARD
var velocity = Vector3.ZERO
var strafe_dir = Vector3.ZERO

var jump_magnitude = 18

var fligth_magnitude = 9
var Fly = 10

var movement_speed = 0
var walk_speed = 3
var run_speed = 12
var acceleration = 7
var angular_acceleration = 7
var super_speed = 50
var speed_increase_increment = 1

func _physics_process(delta):
if Input.is_action_pressed(“Forward”) || Input.is_action_pressed(“Backward”) || Input.is_action_pressed(“Left”) || Input.is_action_pressed(“Right”):

	var h_rot = $Camroot/h.global_transform.basis.get_euler().y 
	
	direction = Vector3(Input.get_action_strength("Left") - Input.get_action_strength("Right"),
				0,
				Input.get_action_strength("Forward") - Input.get_action_strength("Backward")).rotated(Vector3.UP,h_rot).normalized()
	
	if Input.is_action_pressed("Sprint"):
		movement_speed = run_speed
		$AnimationTree.set("parameters/iwr_blend/blend_amount", lerp($AnimationTree.get("parameters/iwr_blend/blend_amount"), 1 , delta * acceleration))
	else: 
		$AnimationTree.set("parameters/iwr_blend/blend_amount", lerp($AnimationTree.get("parameters/iwr_blend/blend_amount"), 0 , delta * acceleration))
		movement_speed = walk_speed
else :
	$AnimationTree.set("parameters/iwr_blend/blend_amount", lerp($AnimationTree.get("parameters/iwr_blend/blend_amount"), -1 , delta * acceleration))
	movement_speed = 0

# faz com que o botão que foi pressionado ative uma ação e se for pressionado de novo desativa a ação
if Input.is_action_just_pressed("E"):
	if run_speed == super_speed:
		run_speed = 12
	else :
		run_speed = super_speed

if is_on_floor():
	if Input.is_action_pressed("Jump"):
		vertical_velocity = jump_magnitude
		


if Input.is_action_just_pressed("Flight"):
	if gravity == 0:
		gravity = 50
		vertical_velocity = 0
		
	else:
		gravity = 0
		vertical_velocity = delta * gravity 
  

velocity = lerp(velocity,direction * movement_speed, delta * acceleration)

move_and_slide(velocity + Vector3.UP * vertical_velocity,Vector3.UP)

if !is_on_floor():
	vertical_velocity -= gravity * delta 
else:
	vertical_velocity = 0
	
$SimplePlayerarma.rotation.y = lerp_angle($SimplePlayerarma.rotation.y,atan2(direction.x,direction.z), delta * angular_acceleration)

func speed_increase():
if $Timer.time_left == 0:
$Timer.start()
super_speed += 1

:bust_in_silhouette: Reply From: theMX89

so, if you wanna make your player fly like in minecraft creativ, you just have to make a key that gives you hight and one that lets you down (space and shift for exemple). W A S D will act the same way as they would on the flor

Thanks Im gonna try this

Luke776 | 2021-06-12 18:08