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
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)
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)
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)