This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

[Godot 2] Problems with a first-person flying code

woopdeedoowoopdeedoo Posts: 109Member
edited November 2018 in 3D

I'm trying to make a simple flying camera for a 3D map editor app (as simple as I can make it). It moves as expected of a FP camera, except that forward/backward moves it toward/away from where it's facing. The code I have was taken from Gokudomatic's eco-fps-controller code (from the AssetLib). I took the fly() function.

It has several problems though, as far as my goal goes. One of them is that it uses the acceleration factor as the friction factor as well. I need to be able to tweak both separately, so I have around medium acceleration and very high friction, for snappy controls (to allow for small taps that move you only by a few inches, while continuous key press still gets you to top speed quickly).

Another problem is that if I use a zero'd Vector3() for the linear_interpolate() function (as the original code uses), it doesn't even transition between speeds. It accelerates to top speed and stops instantly. If I use the velocity vector (as I saw in Jeremy Bullock's youtube videos), it kinda works with very high accel (above 5 or 6), but with low accel it just glides after releasing the keys, and on top of that it doesn't move toward the facing direction unless I'm pressing the keys.

Yet another problem (when using the zero vector for lerping) is that changing the accel value, changes the top speed. Lower accel makes it slower, and vice-versa.

extends KinematicBody

var FLY_SPEED = 10
var FLY_ACCEL = 0.5
#
# (...)
#
func fly(delta):
#   var aim = camera.get_global_transform().basis    # seems to make no difference which of these I use
    var aim = camera.get_camera_transform().basis
#
    var dir = Vector3()
#
    if Input.is_key_pressed(KEY_W):     dir -= aim[2]
    if Input.is_key_pressed(KEY_S):     dir += aim[2]
    if Input.is_key_pressed(KEY_A):     dir -= aim[0]
    if Input.is_key_pressed(KEY_D):     dir += aim[0]
#
    dir = dir.normalized()
#
    var target = dir*FLY_SPEED
    #velocity = Vector3().linear_interpolate( target, FLY_ACCEL*delta )
    velocity = velocity.linear_interpolate( target, FLY_ACCEL*delta )
#
    move(velocity)

I'd like it if someone could help me integrate friction into this, separate from acceleration, and also to understand how to make it work properly.

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.