How to set the orientation of the wheels, instead of adding angles to the orientation?

I am trying to set the orientation of the wheels of a car to the orientation of the road, so that the car can follow a curve. But the car eventually ends up driving off the road, because the road’s orientation is simply added to the wheels’ orientation.

function WheelDetection(hit)
	if hit.Material == Enum.Material.Concrete and hit.Orientation.Y ~= Previous then
		Previous = hit.Orientation.Y
		local Rotation = CFrame.Angles(0,math.rad(Previous),0)
		car.Wheels.FL.Arm.Steer.CFrame = car.Wheels.FL.Base.CFrame.Position * Rotation
		car.Wheels.FR.Arm.Steer.CFrame = car.Wheels.FR.Base.CFrame.Position * Rotation		
	end	
end	
script.Parent.Parent.Wheels.FL.Touched:Connect(WheelDetection)

You need to directly set the rotation of the wheels to the rotation of the road, instead of using CFrame.Angles to create the rotation you should create a CFrame directly from the hit object’s orientation, then use the ToWorldSpace method to convert the rotation from local space to world space and set it as the new rotation for the wheels Xx

1 Like

I don’t understand how I would be able to do that. I’m not good with CFrames tbh.

function WheelDetection(hit)
    if hit.Material == Enum.Material.Concrete and hit.Orientation.Y ~= Previous then
        Previous = hit.Orientation.Y
        local Rotation = CFrame.new(Vector3.new(), hit.Orientation)
        car.Wheels.FL.Arm.Steer.CFrame = car.Wheels.FL.Base.CFrame:ToWorldSpace(Rotation)
        car.Wheels.FR.Arm.Steer.CFrame = car.Wheels.FR.Base.CFrame:ToWorldSpace(Rotation)
    end
end
script.Parent.Parent.Wheels.FL.Touched:Connect(WheelDetection)

“Rotation” prints out as “0, 0, 0, -0, 1, -0, -0, 0, -1, -1, 0, -0” or " 0, 0, 0, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN, NAN", and the wheels don’t follow the curve.

function WheelDetection(hit)
    if hit.Material == Enum.Material.Concrete and hit.Orientation.Y ~= Previous then
        Previous = hit.Orientation.Y
        local Rotation = hit.CFrame:ToEulerAnglesYXZ()
        car.Wheels.FL.Arm.Steer.CFrame = car.Wheels.FL.Base.CFrame * CFrame.Angles(0, Rotation.Y, 0)
        car.Wheels.FR.Arm.Steer.CFrame = car.Wheels.FR.Base.CFrame * CFrame.Angles(0, Rotation.Y, 0)
    end
end
script.Parent.Parent.Wheels.FL.Touched:Connect(WheelDetection)

This time I get this error: "5: attempt to index number with ‘Y’ "

I would very much appreciate it if anyone is able to help.

would be very useful if u give us which line was the error on, idk what line 5 is if u gave us the short part of the script

I renumbered it for here, so line 5 refers to line 5 in the code block.

u should do CFrame.new(vector3) instead of CFrame only so that it instantly updates to the number u specified instead of it updating the already existing number.

I tried doing something like that, but it works absolutely wrong. It appears that it sets the rotation such that the sides of the wheel are aligned with the road, not the actual wheel itself. Adding +90 to Previous helps slightly, but the car understeers and eventually drives off the road.

function WheelDetection(hit)
	if hit.Material == Enum.Material.Concrete and hit.Orientation.Y ~= Previous then
		Previous = hit.Orientation.Y
		local Rotation = CFrame.Angles(0,math.rad(Previous),0)
		car.Wheels.FL.Arm.Steer.CFrame = CFrame.new(car.Wheels.FL.Base.CFrame.Position) * Rotation
		car.Wheels.FR.Arm.Steer.CFrame = CFrame.new(car.Wheels.FR.Base.CFrame.Position) * Rotation
		print(Rotation)		
	end	
end	
script.Parent.Parent.Wheels.FL.Touched:Connect(WheelDetection)