Godot Version
4.6.1
Question
I’ve been tinkering with making a 3D FPS game, one where the player is in zero gravity and as such, can rotate on all axis. I’ve already figured out rotation, movement, all that stuff for this. However, I noticed something odd.
For some reason, the player can not go over 2 m/s, at least when using movement keys (WASD). The thing is, I don’t know why that value is the way it is? In fact there shouldn’t even be a max speed. From my understanding, the code I have should make the player constantly accelerate.
Even stranger is that this max speed doesn’t scale proportionately. For example, if I have the player velocity be calculated via
velocity = velocity.move_toward(direction, delta)
the max speed will be 2. But if I multiply it by 2, making it
velocity = velocity.move_toward(direction, delta) * 2
the max speed is instead 3.87…, not 4 as I would expect.
I will mention that even with this, the direction and rotation of the player character is still functional, and works how I expect/want it to.
I have tried fixing it this past few days, but considering what I am making with the zero-g thing is very unique, I haven’t been able to find many online resources, so I’ve come here.
The code for the ENTIRE player character is:
extends CharacterBody3D
var iscrouching = false
var playervelmultiplyer = 1
var tween = create_tween()
var fireposition
var rotationspeed
var netvel
signal use
const PLAYERVEL = 12.5
const JUMPVEL = 4.5
const SENSITIVITY = 0.01
@onready var camera = $Camera3D
# get gravity from SETTINGS of all things...
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
func _ready():
# mouse cursor is gone, otherwise player could only turn so much until cursor reaches sides of viewport
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
if global.haspistol == true:
var pistolpath = preload("res://scenes/weapons/pistol.tscn").instantiate()
add_child(pistolpath)
func _unhandled_input(event):
if event is InputEventMouseMotion:
# rotate camera by how much the mouse is moving * mouse sensitivity
rotationspeed = event.relative.length() * SENSITIVITY
event.relative = event.relative.normalized()
rotate_object_local(Vector3(-event.relative.y, -event.relative.x, 0), rotationspeed)
func _physics_process(delta):
if Input.is_action_just_pressed("flashlight"):
if $playerflashlight.visible == true:
$playerflashlight.visible = false
else:
$playerflashlight.visible = true
if Input.is_action_pressed("rotateleft"):
rotate_object_local(Vector3(0,0,1),0.03)
if Input.is_action_pressed("rotateright"):
rotate_object_local(Vector3(0,0,-1),0.03)
if Input.is_action_just_pressed("lmb"):
emit_signal("use")
# apply recoil to player
self.velocity += self.transform.basis * Vector3( 0, 0, 1) * 0.1
if Input.is_action_pressed("crouch"):
iscrouching = true
playervelmultiplyer = 0.25
if tween:
tween.kill()
tween = create_tween().set_parallel()
tween = create_tween().set_parallel()
tween.tween_property($CollisionShape3D,"scale",Vector3(1,0.5,1),0.05) # tween everything so no weridy
tween.tween_property($CollisionShape3D,"position",Vector3(0,0.5,0),0.05)
else:
iscrouching = false
playervelmultiplyer = 1
if tween:
tween.kill()
tween = create_tween().set_parallel()
tween = create_tween().set_parallel()
tween.tween_property($CollisionShape3D,"scale",Vector3(1,1,1),0.05)
tween.tween_property($CollisionShape3D,"position",Vector3(0,0,0),0.05)
# x/z movement
# gets avg of WASD inputs, otherwise can only move one at a time, no up or down movement
var input_plane = Input.get_vector("moveleft","moveright","moveforward","movebackward")
var input_vertical = Input.get_axis("movedownward","moveupward")
# go where we are facing
var direction = (self.transform.basis * Vector3(input_plane.x,input_vertical,input_plane.y)).normalized()
netvel = abs(velocity.x) + abs(velocity.z) + abs(velocity.z)
$debug.text = str(netvel)
# move
if direction:
velocity = velocity.move_toward(direction, delta)
else: # slow to stop if no inputs
if Input.is_action_pressed("mmb"):
velocity.x -= lerp(velocity.x, direction.x * PLAYERVEL * playervelmultiplyer, delta) * 0.025
velocity.z -= lerp(velocity.z, direction.z * PLAYERVEL * playervelmultiplyer, delta) * 0.025
velocity.y -= lerp(velocity.y, direction.y * PLAYERVEL * playervelmultiplyer, delta) * 0.025
else:
velocity.x -= lerp(velocity.x, direction.x * PLAYERVEL * playervelmultiplyer, delta) * 0.001
velocity.z -= lerp(velocity.z, direction.z * PLAYERVEL * playervelmultiplyer, delta) * 0.001
velocity.y -= lerp(velocity.y, direction.y * PLAYERVEL * playervelmultiplyer, delta) * 0.001
move_and_slide()