62 lines
1.4 KiB
GDScript3
62 lines
1.4 KiB
GDScript3
![]() |
|
||
|
extends VehicleBody
|
||
|
|
||
|
# Member variables
|
||
|
const STEER_SPEED = .25
|
||
|
const STEER_LIMIT = .3
|
||
|
|
||
|
var steer_angle = 0
|
||
|
var steer_target = 0
|
||
|
var view_sensitivity = 0.3;
|
||
|
|
||
|
export var engine_force = 20
|
||
|
|
||
|
|
||
|
func _fixed_process(delta):
|
||
|
if (Input.is_action_pressed("ui_left")):
|
||
|
steer_target = -STEER_LIMIT
|
||
|
elif (Input.is_action_pressed("ui_right")):
|
||
|
steer_target = STEER_LIMIT
|
||
|
else:
|
||
|
steer_target = 0
|
||
|
|
||
|
if (Input.is_action_pressed("ui_up")):
|
||
|
set_engine_force(engine_force)
|
||
|
else:
|
||
|
set_engine_force(0)
|
||
|
|
||
|
if (Input.is_action_pressed("ui_down")):
|
||
|
#set_brake(1)
|
||
|
set_engine_force(-engine_force)
|
||
|
|
||
|
if (Input.is_action_pressed("driver_break")):
|
||
|
set_brake(1.0)
|
||
|
else:
|
||
|
set_brake(0.0)
|
||
|
|
||
|
if (steer_target < steer_angle):
|
||
|
steer_angle -= STEER_SPEED*delta
|
||
|
if (steer_target > steer_angle):
|
||
|
steer_angle = steer_target
|
||
|
elif (steer_target > steer_angle):
|
||
|
steer_angle += STEER_SPEED*delta
|
||
|
if (steer_target < steer_angle):
|
||
|
steer_angle = steer_target
|
||
|
|
||
|
set_steering(steer_angle)
|
||
|
|
||
|
|
||
|
func _ready():
|
||
|
set_fixed_process(true)
|
||
|
|
||
|
func _input(ie):
|
||
|
if ie.type == InputEvent.MOUSE_MOTION:
|
||
|
var yaw = rad2deg(get_node(".").get_rotation().y);
|
||
|
var pitch = rad2deg(get_node("Camera").get_rotation().x);
|
||
|
|
||
|
yaw = fmod(yaw - ie.relative_x * view_sensitivity, 360);
|
||
|
pitch = max(min(pitch - ie.relative_y * view_sensitivity, 90), -90);
|
||
|
|
||
|
get_node(".").set_rotation(Vector3(0, deg2rad(yaw), 0));
|
||
|
get_node("Camera").set_rotation(Vector3(deg2rad(pitch), 0, 0));
|