Collisions not working properly in 3D

Godot Version

Godot 4.2.1

Question

Hello! I’m making a racing game similar to Mario Kart. I’m doing the tracks for it, and for their collision I used “Create Trimesh Collision Sibling”
imagen_2024-06-30_195710351

It seemed like it worked but then the car doesn’t work properly, it jumps randomly (video of what was happening: video.mp4 - Google Drive)
Here’s the car’s code if it helps:

extends Node3D

@onready var ball = $RigidBody3D
@onready var car_mesh = $car_mesh
@onready var raycast = $car_mesh/RayCast3D
@onready var cam_pivot = $car_mesh/cam_pivot
@onready var cam = $car_mesh/cam_pivot/Camera3D

@onready var body_mesh = $car_mesh/Car
@onready var left_wheel = $car_mesh/left_wheel
@onready var right_wheel = $car_mesh/right_wheel
@onready var left_back_wheel = $car_mesh/leftBack_wheel
@onready var right_back_wheel = $car_mesh/rightBack_wheel

@onready var particles = $car_mesh/GPUParticles3D

@export var sphere_offset = Vector3(0, -0.5, 0)
@export var acceleration = 500
@export var steering = 25
@export var turn_speed = 3
@export var turn_stop_limit = 5
@export var body_tilt = 400

var direction_needed = true
var is_drifting = false
var drift_dir = 0
var time_drifting = 0
var miniturbo = 0
var applying_force = false


var LookAt


#Car Stats
@export var miniturbo_get_speed = 1


func _ready():
	raycast.add_exception(ball)
	LookAt = global_position

func _physics_process(delta):
	#Getting Inputs
	var direction_fw = Input.get_axis("accel", "deccel")
	var direction_side =Input.get_axis("right", "left")
	#If the car is on the floor (the raycast is colliding) we can accelerate/brake
	#Making the Inputs greater with variables
	direction_fw *= acceleration
	direction_side *= deg_to_rad(steering)
	
	#The car mesh is placed correctly
	#We apply the force
	car_mesh.global_position = ball.global_position + sphere_offset
	if applying_force:
		ball.apply_central_force(-car_mesh.global_basis.z * -acceleration * 2)
	elif ball.linear_velocity.length() < 120:
		ball.apply_central_force(-car_mesh.global_basis.z * direction_fw)
	
	
	left_wheel.rotation.x += ball.linear_velocity.length() * 0.005
	right_wheel.rotation.x += ball.linear_velocity.length() * 0.005
	left_back_wheel.rotation.x += ball.linear_velocity.length() * 0.005
	right_back_wheel.rotation.x += ball.linear_velocity.length() * 0.005
	
	
	
	
	
	#Rotating the car
	if is_drifting:
		if ball.linear_velocity.length() > turn_stop_limit:
			var new_basis = car_mesh.global_transform.basis.rotated(car_mesh.global_transform.basis.y, drift_dir)
			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()
	else:
		if ball.linear_velocity.length() > turn_stop_limit:
			var new_basis = car_mesh.global_transform.basis.rotated(car_mesh.global_transform.basis.y, direction_side)
			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()
	
	#Drifting the wheels
	left_wheel.rotation.y = lerp(left_wheel.rotation.y, direction_side + drift_dir, delta * 15)
	right_wheel.rotation.y = lerp(right_wheel.rotation.y, direction_side + drift_dir, delta * 15)
	
	#Tilting the car slightly for a cool visual effect
	var tilt = -direction_side * ball.linear_velocity.length() / body_tilt
	body_mesh.rotation.z = lerp(body_mesh.rotation.z, tilt, delta * 10)
	
	
	#Drifting
	if Input.is_action_pressed("drift"):
		acceleration = 110
		steering = 50
		is_drifting = true
		if direction_needed:
			if Input.get_axis("right","left") > 0:
				drift_dir = 1
			if Input.get_axis("right","left") < 0:
				drift_dir = -1
			if Input.get_axis("right","left") == 0:
				drift_dir = 0
			direction_needed = false
		if drift_dir == 0:
			direction_needed = true
		if drift_dir * direction_side > 0:
			turn_speed = 2
		elif drift_dir * direction_side < 0:
			turn_speed = 0
		elif drift_dir * direction_side == 0:
			turn_speed = 1
	else:
		acceleration = 100
		is_drifting = false
		steering = 25
		turn_speed = 3
	
	if Input.is_action_just_released("drift"):
		direction_needed = true
		time_drifting = 0
		drift_dir = 0
		applying_force = true
		if miniturbo != 0:
			$RigidBody3D/Timer.wait_time = miniturbo
			$RigidBody3D/Timer.start()
			miniturbo = 0
		else:
			applying_force = false
	
	
	if raycast.is_colliding():
		ball.gravity_scale = 6
	else:
		ball.gravity_scale = 10
	
	
	if is_drifting and ball.linear_velocity.length() > 5 and drift_dir != 0:
		time_drifting += delta
		if time_drifting > 1 / miniturbo_get_speed:
			print("azul")
			particles.emitting = true
			miniturbo = 0.4
		if time_drifting > 2.3 / miniturbo_get_speed:
			print("rojo")
			miniturbo = 0.7
		if time_drifting > 6 / miniturbo_get_speed:
			print("morado")
			miniturbo = 1
		
		
		if drift_dir > 0:
			particles.rotation.y = 0
		if drift_dir < 0:
			particles.rotation.y = 180
		
	
	print(miniturbo)
	
	
	
	#Camera control
	LookAt = LookAt.lerp(car_mesh.global_position + (ball.linear_velocity / 5), delta * 5)
	
	cam_pivot.global_position = cam_pivot.global_position.lerp(car_mesh.global_position, delta * 20)
	cam_pivot.transform = cam_pivot.transform.interpolate_with(car_mesh.global_transform, delta * 5)
	cam.look_at(LookAt)
	cam.rotation_degrees.x = cam.rotation_degrees.x + 20


func _on_timer_timeout():
	applying_force = false
	miniturbo = 0
	print("ya")

Then I tried making cubes and placing them manually across the track and it worked, except in the elevetions. I’m sure there is an easier way to do this and that it’s possible to have elevations without the game bugging.

If you have any idea of how to make this, please say it. Thank you!