RayCast car suspension

Godot Version

4.2

Question

I’m trying to make a custom car physics using raycasts to move a rigid body. I’ve watch every video about it but I can’t quite figure it out since most of them are on Godot 3

This is what I’m trying to achieve:

This is what I have right now:

It jitters, doesn’t go all the way up. Can anyone please help me with that? This is my code:

Wheel code:

func _physics_process(delta):
	getVect()
	if is_on_ground():
		var collision_point = to_local(raycast.get_collision_point())
		var ray_target = raycast.target_position
		#offset from desired spring length
		displacement = ray_target - collision_point
		#used by parent to apply force
		susp_force = (displacement * susp_power) - (-wheelVector * susp_damp)

func is_on_ground():
	return raycast.is_colliding()

func getVect():
	var diff = get_global_position() - last_pos
	wheelVector = diff.normalized()
	last_pos = get_global_position()

Car code:

func _physics_process(delta):
	for i in wheels.size():
		if wheels[i].is_on_ground():
			var wheel_point = wheels[i].position
			var susp_force = wheels[i].susp_force
			var wheel_displacement = wheels[i].displacement
			#to hold the car
			apply_central_impulse(-wheel_displacement * 1200 * delta)
			#suspention activation
			apply_impulse(-susp_force, wheel_point)

Nevermind, I found a great tutorial for Godot 4!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.