-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
car.gd
50 lines (42 loc) · 1.83 KB
/
car.gd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
extends RigidBody3D
var sphere_offset = Vector3.DOWN
@export var acceleration = 35.0
@export var steering = 19.0
@export var turn_speed = 4.0
@export var turn_stop_limit = 0.75
@export var body_tilt = 35
var speed_input = 0
var turn_input = 0
@onready var car_mesh = $CarMesh
@onready var body_mesh = $CarMesh/suv2
@onready var ground_ray = $CarMesh/RayCast3D
@onready var right_wheel = $CarMesh/suv2/wheel_frontRight
@onready var left_wheel = $CarMesh/suv2/wheel_frontLeft
#func _ready():
# ground_ray.add_exception(self)
func _physics_process(delta):
car_mesh.position = position + sphere_offset
if ground_ray.is_colliding():
apply_central_force(-car_mesh.global_transform.basis.z * speed_input)
func _process(delta):
if not ground_ray.is_colliding():
return
speed_input = Input.get_axis("brake", "accelerate") * acceleration
turn_input = Input.get_axis("steer_right", "steer_left") * deg_to_rad(steering)
right_wheel.rotation.y = turn_input
left_wheel.rotation.y = turn_input
if linear_velocity.length() > turn_stop_limit:
var new_basis = car_mesh.global_transform.basis.rotated(car_mesh.global_transform.basis.y, turn_input)
car_mesh.global_transform.basis = car_mesh.global_transform.basis.slerp(new_basis, turn_speed * delta)
car_mesh.global_transform = car_mesh.global_transform.orthonormalized()
var t = -turn_input * linear_velocity.length() / body_tilt
body_mesh.rotation.z = lerp(body_mesh.rotation.z, t, 5.0 * delta)
if ground_ray.is_colliding():
var n = ground_ray.get_collision_normal()
var xform = align_with_y(car_mesh.global_transform, n)
car_mesh.global_transform = car_mesh.global_transform.interpolate_with(xform, 10.0 * delta)
func align_with_y(xform, new_y):
xform.basis.y = new_y
xform.basis.x = -xform.basis.z.cross(new_y)
# xform.basis = xform.basis.orthonormalized()
return xform.orthonormalized()