Hey everyone,
I'm having some strange behavior occur with my KinematicBody character. First, when I first press one of the arrow keys, my character will begin to move, but when I release the keys my character will just keep going, and even when it hits a wall it will just continue to slide indefinitely.
Secondly, it's like my input isn't being listened to, and it takes several seconds for my player character to turn around and it does so in a strange sweeping motion. I thought it was my rotation code, but when I commented it out, the effect remained, my player took several seconds to respond to the opposite input.
Also, I've noted that in the Godot docs it says that "forward" is in the negative Z axis, yet when I designed my player, I had to put its nose and arms (my indicators of which way it was actually facing) on the positive X side. Maybe it's where I placed my camera? Not sure.
Here's a video showing the issues:
And here's my code:
extends KinematicBody
# inspector accessible variables
export var Gravity = -20
export var GravityMultiplier = 1
export var MoveSpeed = 20
export var TurnSpeed = 20
export var JumpHeight = 10
export var FlightHeight = 10
export var FlightSpeed = 10
export var CanDoubleJump = false
# private variables
var thisGravity = 0
var direction = Vector3()
var velocity = Vector3()
# our input floats
var h
var v
# on startup
func _ready():
set_physics_process(true)
# Updates the game, mostly physics stuff
func _physics_process(delta):
_get_input(delta)
_apply_gravity(delta)
_jump()
_move_player()
# moves the player
func _move_player():
# set the player's rotation to the values as given by the joystick
# first flip vertical axis, possible bug with rotations
var rotation_axis = Vector2(direction.x, direction.z * -1)
set_rotation(Vector3(0, rotation_axis.angle(), 0))
direction = direction.normalized()
direction = direction * MoveSpeed
# add the input and gravity values to the velocity Vector
velocity = Vector3(direction.x, thisGravity, direction.z)
# move the player
velocity = move_and_slide(velocity, Vector3(0, 1, 0))
# get and store input in our h and v variables
func _get_input(delta):
# h = Input.get_joy_axis(0, JOY_AXIS_0)
# v = Input.get_joy_axis(0, JOY_AXIS_1)
if Input.is_action_pressed("ui_left"):
direction.x -= 1
if Input.is_action_pressed("ui_right"):
direction.x += 1
if Input.is_action_pressed("ui_up"):
direction.z -= 1
if Input.is_action_pressed("ui_down"):
direction.z += 1
# apply gravity, pass in delta value from main physics process
func _apply_gravity(delta):
if is_on_floor():
thisGravity = 0
else:
thisGravity += Gravity * GravityMultiplier * delta
# jump our player
func _jump():
if is_on_floor() and Input.is_action_pressed("ui_select"):
thisGravity += JumpHeight
If anyone can offer my any insight, I'd sure appreciate it! Thanks!