Angling car wheels toward Rig not working properly

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    Hello! I am trying to make a car system for my Movie Module so cars go towards a specific part so they drive straight on roads.

  2. What is the issue? Include screenshots / videos if possible!
    My problem is that… It can turn in the right direction, but when I try to clamp the wheels the clamping does not work. I tried converting it to a 0 to 360 type rotation instead of 180 and -180, but this does not work either and I am stumped.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

My code is shown below.

car = script.Parent
wheels = car:WaitForChild("Wheels")

mover = car:WaitForChild("Mover")
base = car:WaitForChild("Base"):WaitForChild("Base")

local steer = mover:WaitForChild("Steer")
local throttle = mover:WaitForChild("Throttle")

local pos = script:WaitForChild("Position")

part2 = Instance.new("Part")
part2.Anchored = true
part2.Parent = workspace

function MoveCarTo(Position:Vector3)
	local lookAt = CFrame.lookAt(base.Position, Position)
	
	local newPart = Instance.new("Part")
	newPart.CFrame = lookAt
	local y = newPart.Orientation.Y
	
	part2.Position = base.Position+Vector3.new(0, 10, 0)
	part2.Orientation = newPart.Orientation
	
	newPart:Remove()
	
	if y < 0 then
		y = 180+math.abs(y)
	end
	
	local value = y
	print(value)
	
	wheels:WaitForChild("Front"):WaitForChild("WL"):WaitForChild("Connection"):WaitForChild("HingeAttachment0").WorldOrientation = Vector3.new(0, math.clamp(value, 45, 315), 0)
	wheels:WaitForChild("Front"):WaitForChild("WR"):WaitForChild("Connection"):WaitForChild("HingeAttachment0").WorldOrientation = Vector3.new(0, math.clamp(value, 45, 315)+180, 0)
end

while wait() do
	workspace.Car.MoveToPosition.Position.Value = workspace.Rig:GetPivot().Position -- This just tells the car to go to a rig dummy character in the workspace.
	MoveCarTo(pos.Value)
end

apologies if the way I use CFrames isn’t the best, they aren’t my strong suit and I don’t like using them so I often use parts to convert.

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

4 Likes

Here’s a modified snippet:

-- Replace this line
-- wheels:WaitForChild("Front"):WaitForChild("WL"):WaitForChild("Connection"):WaitForChild("HingeAttachment0").WorldOrientation = Vector3.new(0, math.clamp(value, 45, 315), 0)

-- With this
local currentRotation = wheels:WaitForChild("Front"):WaitForChild("WL"):WaitForChild("Connection"):WaitForChild("HingeAttachment0").WorldOrientation
local desiredRotation = Vector3.new(0, value, 0)
local clampedRotation = Vector3.new(0, math.clamp(desiredRotation.Y - currentRotation.Y, -45, 45) + currentRotation.Y, 0)
wheels:WaitForChild("Front"):WaitForChild("WL"):WaitForChild("Connection"):WaitForChild("HingeAttachment0").WorldOrientation = clampedRotation

Give this a shot and let me know if it helps! If you encounter any issues or have more details to share, feel free to update the question.

2 Likes

Okay, I will try this. i will keep you updated.

Thanks for the help!

I had reset my code to be like how I have it in my post and add your solution but uhh…


unfortunately it did… not work.

1 Like

Could give some details on how it didn’t work? Any error messages or unexpected behavior you observed would be super helpful.

the output did not display anything off… The car wheels were just spinning randomly as shown in the video

I was checking your original post and don’t see a video anywhere?

In the meantime, add some prints to the code so we can find out what’s going on at each step.

print("Current Rotation:", currentRotation)
print("Desired Rotation:", desiredRotation)
print("Clamped Rotation:", clampedRotation)
1 Like

wait, i think this is on me actually, I got it to stop spinning but the right wheel is facing in the incorrect direction. I will try this to see what it prints. In the meantime, can you tell me how to flip the direction so the right wheel doesn’t spin?

okay, so i implemented what you said but nothing off the values seems off. Its just printing the orientations. Would you like a screenshot?

Lol no worries! We’ll figure this out together… If the values are printing correctly and the issue is with the right wheel facing in the incorrect direction, you might need to adjust the sign of the clamped rotation for the right wheel.

-- Replace this line
-- wheels:WaitForChild("Front"):WaitForChild("WR"):WaitForChild("Connection"):WaitForChild("HingeAttachment0").WorldOrientation = Vector3.new(0, math.clamp(value, 45, 315)+180, 0)

-- With this
local rightWheelClampedRotation = Vector3.new(0, math.clamp(value, -315, -45) + currentRotation.Y + 180, 0)
wheels:WaitForChild("Front"):WaitForChild("WR"):WaitForChild("Connection"):WaitForChild("HingeAttachment0").WorldOrientation = rightWheelClampedRotation

uh. I don’t know where to put this. You didn’t replace my old code at the top.

ill be back

Okay, I am back. I would like to try to state my problem better. My main problem is simply put, the wheels often do not point towards the target. Furthermore, another bug is me not completely understanding how to clamp Orientations properly as sometimes it just snaps to something else which makes no sense to me. before I tackle any other problems, Is there a reliable way to clamp an orientation of a part without it freaking out/facing incorrectly?

NEVERMIND, I found the issue. It was on me.

This is my new code for anyone that is having this problem!

car = script.Parent
wheels = car:WaitForChild("Wheels")

mover = car:WaitForChild("Mover")
base = car:WaitForChild("Base"):WaitForChild("Base")

local steer = mover:WaitForChild("Steer")
local throttle = mover:WaitForChild("Throttle")

local pos = script:WaitForChild("Position")

part2 = Instance.new("Part")
part2.Anchored = true
part2.Parent = workspace

function MoveCarTo(Position:Vector3)
	local WL = wheels:WaitForChild("Front"):WaitForChild("WL"):WaitForChild("Connection"):WaitForChild("HingeAttachment0")
	local WR = wheels:WaitForChild("Front"):WaitForChild("WR"):WaitForChild("Connection"):WaitForChild("HingeAttachment0")
	
	local lookAt = CFrame.lookAt(base.Position, Position)
	
	local newPart = Instance.new("Part")
	newPart.CFrame = lookAt
	
	part2.Position = base.Position+Vector3.new(0, 10, 0)
	part2.Orientation = newPart.Orientation
	
	newPart:Remove()
	
	local x,y,z = newPart.CFrame:ToOrientation()
	
		
	WL.WorldOrientation = Vector3.new(0, math.deg(y), 0)
	local value = math.clamp(WL.Orientation.Y, -35, 35)
	WL.Orientation = Vector3.new(0, value, 0)
	
	WR.WorldOrientation = Vector3.new(0, math.deg(y)-180, 0)
	local value = math.clamp(WR.Orientation.Y, -35, 35)
	WR.Orientation = Vector3.new(0, value, 0)
end

while wait() do
	workspace.Car.MoveToPosition.Position.Value = workspace.Rig:GetPivot().Position
	MoveCarTo(pos.Value)
end

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