I want my character to fly in the direction my mouse points

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

I want to make my player flyin such where my mouse/camera view is pointed my player will fly that direction if I am pressing the “W” key.

Here is my script:

extends KinematicBody

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

var jump_magnitude = 18
var jump_blend = “parameters/jump_blend/blend_position”
var ag_transition = “parameters/ag_transition/current”
var ag_transition_active = “parameters/jump_blend/acitve”

var fligth_magnitude = 2
var velocity_fly = 0

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 is_on_floor():
	
			if Input.is_action_pressed("Jump"):
				vertical_velocity = jump_magnitude 
				$AnimationTree.set(ag_transition,0)
	
			else :
				$AnimationTree.set(ag_transition,1)
				$AnimationTree.set(jump_blend, lerp($AnimationTree.get(jump_blend), vertical_velocity/jump_magnitude, delta * 10 ))

if gravity <0:
	gravity = 50

if Input.is_action_just_pressed("Flight"):
	if gravity == 0:
		gravity = 50
		vertical_velocity = 0
		run_speed =12
		walk_speed = 3
		
	else:
		gravity = 0
		vertical_velocity = delta * gravity 
		run_speed = 50
		walk_speed = 5

if gravity ==0 and !is_on_floor():
	if Input.is_action_pressed("Up"):
		velocity.y  += fligth_magnitude * 2
	if Input.is_action_pressed("E"):
				velocity.y -= fligth_magnitude * 2
	elif is_on_floor():
		velocity.y = 0
		

if Input.is_action_pressed("r") and !is_on_floor():
	gravity = 1
	movement_speed = 30
else:
	gravity = 50




  

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
	
$Mesh/MineCharatertrue/SimplePlayerarma.rotation.y = lerp_angle($Mesh/MineCharatertrue/SimplePlayerarma.rotation.y,atan2(direction.x,direction.z), delta * angular_acceleration)
:bust_in_silhouette: Reply From: Yuminous

Hi there Luke, there are many many ways to do this depending on what you need.

I couldn’t manage to parse your code, so I have made a demo project which has one very rudimentary solution to your requirement of the character flying toward the mouse cursor. It also has basic 3D navigation included as well, but I think you have that sorted anyway.

You can check it out over on Github if you’d like, or just download it directly.
“3.3 → Rudimentary 3D Movement incl. Flight”

How it works is like this: the same ground movement commands are naturally used for airbourne maneuvering as well, but the ascent/descent is simply ripped straight from the current angle value of the camera (or gimbal) X-axis rotation (approximately where the player is pointing upward) and directly calculated into the player’s vertical translation.

Like I said, it’s really rudimentary, but it does work pretty well from what I’ve tested. You can probably tune this to the point where players don’t really mind about certain funky quirks about the flight model.

Absolutely feel free to copy anything from this demo or any ideas to get flight working in the game you’re creating.

Good luck!

Thank u Im gonna try this

Luke776 | 2021-07-09 02:18