Hello there,
I'm new to the Godot community and stil learning. I have written a third person player script following a tutorial. When testing only the defined key for moving backwards(s) works. All other directions don't seem to do anything at all. Why is that?
Here is my code:
extends KinematicBody
#Player speed control
export var speed : float = 20
#Player accelaration control
export var accelaration : float = 15
#Player air_accelaration control
export var air_acceleration : float = 5
#gravity control(rate per second at which the character falls)
export var gravity : float = 0.98
#maximum falling speed control
export var max_terminal_velocity : float = 54
#jump force control
export var jump_power : float = 20
#mouse sensitivity control
export(float, 0.1, 1) var mouse_sensitivity : float = 0.3
#min and max camera movement limit
export(float, -90, 0) var min_pitch : float = -90
export(float, 0, 90) var max_pitch : float = 90
#Player current speed
var velocity : Vector3
var y_Velocity : float
#remember element references
onready var camera_pivot = $CameraPivot
onready var camera = $CameraPivot/CameraBoom/PlayerCamera
#func _ready() gets called after elements have loaded
func _ready():
#Capture mouse input and hide cursor
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
#func _process(delta) gets called every frame
#delta stores time elapsed since last call
func _process(delta):
#if ui_cancel(default: ESC) button pressed -
if Input.is_action_just_pressed("ui_cancel"):
#-stop capture mouse and make cursor visible
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
#func _input(event) gets called with every input
func _input(event):
#if mouse is moved
if event is InputEventMouseMotion:
#change "Player" y-rotation to relative x-movement of mouse * the mouse sensitivity
rotation_degrees.y -= event.relative.x * mouse_sensitivity
#change "camera_pivot" x-rotation to realtive y-movement of mouse * the mouse sensitivity
camera_pivot.rotation_degrees.x -= event.relative.y * mouse_sensitivity
#limit camera_pivot x-rotation
camera_pivot.rotation_degrees.x = clamp(camera_pivot.rotation_degrees.x, min_pitch, max_pitch)
#func _physics_process(delta) gets called 60 times per second (default value)
#delta stores the time elapsed since last call
func _physics_process(delta):
#calculate movement for player
movement_calc(delta)
#func movement_process(delta) gets called with every call of _physics_process
#delta stores time elapsed since last call of _physics_process
func movement_calc(delta):
#Player direction control
var direction = Vector3()
#while "move_forward"(default: W) is pressed
if Input.is_action_pressed("move_forward"):
#change direction in negative z
direction -= transform.basis.z
#while "move_backward"(default: S) is pressed
if Input.is_action_pressed("move_backward"):
#change direction in positive z
direction += transform.basis.z
#while "move_left"(default: A) is pressed
if Input.is_action_pressed("move_left"):
#change direction in negative x
direction -= transform.basis.x
#while "move_right"(default: D) is pressed
if Input.is_action_pressed("move_right"):
#change direction in positive x
direction += transform.basis.x
#normalize Vector
direction = direction.normalized()
#calculate velocity
var accel = accelaration if is_on_floor() else air_acceleration
velocity = velocity.linear_interpolate(direction * speed, accel * delta)
#if player on floor
if is_on_floor():
#make him stick
y_Velocity = -0.01
else:
#make him fall
y_Velocity = clamp(y_Velocity - gravity, -max_terminal_velocity, max_terminal_velocity)
#if "jump"(default: space) was pressed
if Input.is_action_just_pressed("jump") and is_on_floor():
#accelearte him in the air
y_Velocity = jump_power
#apply jump
velocity.y = y_Velocity
velocity = move_and_slide(velocity, Vector3.UP)
I hope the comments don't make it to unreadable, I added them for my understanding, I guess copying the code into the godot editor works best. Feel free to correct me on the comments since I am still learning and trying to wrap my head around the godot engine and gdscript.