# Mario kart like physics and controls

4.2.2

### Question

Hey all, i’m new to the forums,
i’m trying to make a mario kart like control scheme, this is my first time trying godot and i’m having a bit of a problem.
my idea is to allign the kart to the surface i’ts on, using it’s sphere collision to find the point it’s touching. (in the future i will separate the road, wall, and offroad collision meshes, so it will be able to go different speeds or bounce if it touches those ),
but everithing i’ve tried is glitchy.

this is my code for now, dont mind drift and jump, thats all WIP,
altho any help is appreciated!

and everything that comes out of this is free for anyone to use, i just want to make a meme game for me and my friends
i’d even upload the project if i could

``````extends CharacterBody3D

var turning_speed = 1
var drift_speed = 2
var acceleration = 40.0
var braking = 20.0
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")
var friction = [2, 5]  # forward friction, lateral friction
var local_velocity = Vector3()

update_local_velocity()

func _physics_process(delta):
# Add gravity if not on the floor
if not is_on_floor():
velocity.y -= gravity * delta

# Handle jumping
if is_on_floor():
if Input.is_action_just_pressed("jump"):
fake_jump()

update_local_velocity()

# Adjust camera FOV based on forward speed
if local_velocity.z > 0:
\$Camera3D.set_fov(80 + local_velocity.z / 4)

# Handle input actions
handle_movement(delta)

# Update global velocity
update_global_velocity()

# Move the character
move_and_slide()

func handle_movement(delta):

if Input.is_action_pressed("forward"):
local_velocity.z += acceleration * delta
elif Input.is_action_pressed("back"):
local_velocity.z -= braking * delta

if Input.is_action_pressed("jump"):
apply_friction(friction[0], friction[0])
handle_turning(drift_speed,delta)
else:
apply_friction(friction[0], friction[1])
handle_turning(turning_speed,delta)

if Input.is_action_just_released("jump"):
boost(1.0)

func apply_friction(forward_friction, lateral_friction):
local_velocity.z -= local_velocity.z * (forward_friction/ 100.0)
local_velocity.x -= local_velocity.x * (lateral_friction/ 100.0)

func handle_turning(speed,delta):
if Input.is_action_pressed("left"):
rotate_y(speed * delta)
elif Input.is_action_pressed("right"):
rotate_y(-speed * delta)

func update_global_velocity():
velocity = basis.x * local_velocity.x + basis.y * local_velocity.y + basis.z * local_velocity.z

func update_local_velocity():
local_velocity = Vector3(
basis.x.dot(velocity),
basis.y.dot(velocity),
basis.z.dot(velocity)
)

func global_velocity_to_local(global_vel):
return Vector3(
basis.x.dot(global_vel),
basis.y.dot(global_vel),
basis.z.dot(global_vel)
)

func boost(time):
if local_velocity.z < 50:
local_velocity.z = 50

func fake_jump():
velocity.y += 10
``````