- Edited
How do I lock the linear movement of the rigid body simulating the wheel in the X and Z axis? In the following post are images depicting how I've set the chassis up so far. Here's the code I'm currently using:
extends RigidBody3D
@export var wheel_max_compression = 0.5
@export var suspension_stiffness = 20
@onready var fr_ray_cast: RayCast3D = $RayCastFrontRight
@onready var fl_ray_cast: RayCast3D = $RayCastFrontLeft
@onready var rr_ray_cast: RayCast3D = $RayCastRearRight
@onready var rl_ray_cast: RayCast3D = $RayCastRearLeft
@onready var fr_wheel: RigidBody3D = $RayCastFrontRight/Wheel
@onready var fl_wheel: RigidBody3D = $RayCastFrontLeft/Wheel
@onready var rr_wheel: RigidBody3D = $RayCastRearRight/Wheel
@onready var rl_wheel: RigidBody3D = $RayCastRearLeft/Wheel
func _physics_process(delta):
fr_ray_cast.force_raycast_update()
fl_ray_cast.force_raycast_update()
rr_ray_cast.force_raycast_update()
rl_ray_cast.force_raycast_update()
var fr_comp = wheel_max_compression - fr_ray_cast.get_collision_point().y
var fl_comp = wheel_max_compression - fl_ray_cast.get_collision_point().y
var rr_comp = wheel_max_compression - rr_ray_cast.get_collision_point().y
var rl_comp = wheel_max_compression - rl_ray_cast.get_collision_point().y
var fr_force = suspension_stiffness * fr_comp
var fl_force = suspension_stiffness * fl_comp
var rr_force = suspension_stiffness * rr_comp
var rl_force = suspension_stiffness * rl_comp
apply_central_force(fr_force * fr_ray_cast.global_transform.basis.y)
apply_central_force(fl_force * fl_ray_cast.global_transform.basis.y)
apply_central_force(rr_force * rr_ray_cast.global_transform.basis.y)
apply_central_force(rl_force * rl_ray_cast.global_transform.basis.y)