How would I make the wheels spin in the same direction the player is moving?

Right now the wheels only spin right
(And yes I am remaking an NES game)

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

local ts = game:GetService("TweenService")
local ti = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out,-1)

local propertyToTween = {Orientation = Vector3.new(0, 0, -360)}

local RW = script.Parent.RightWheel

local tween = ts:Create(script.Parent.LeftWheel, ti, propertyToTween)
local tween2 = ts:Create(RW, ti, propertyToTween)

humanoid.Running:Connect(function(speed)
	if speed > 0 then
		tween:Play()
		tween2:Play()
		else 
		tween:Pause() 
		tween2:Pause()
	end
end)
4 Likes

The Humanoid has a read-only property called MoveDirection which tells you
which direction the player is moving in. For example, when my character moves left across the X axis, the MoveDirection.X is -1 (which tells me that I’m moving left relative to the world space)
image

and when I move right, the MoveDirection.X is 1 (moving right relative to the world space)
image

You can use this logic in your code to move the wheels left or right depending on the direction the player is moving

Like this?(It isn’t working)

if humanoid.MoveDirection.x == 1 then
	local propertyToTween = {Orientation = Vector3.new(0, 0, -360)}
	end
		if humanoid.MoveDirection.x == -1 then
		local propertyToTween = {Orientation = Vector3.new(0, 0, 360)}
	end
1 Like

Something like this should work?

Humanoid.Running:Connect(function()
	-- check to see which axis your 2d game is in, it's either gonna be x or z
	if Humanoid.MoveDirection.X > 0.01 then
		print("right")
	elseif Humanoid.MoveDirection.X < -0.01 then
		print("left")
	end
end)
1 Like

The code works but, when I replaced the print with the code:

	if humanoid.MoveDirection.X > 0.01 then
		propertyToTween.Orientation.Z = -360
		elseif humanoid.MoveDirection.X < -0.01 then
		propertyToTween.Orientation.Z = 360
		end
	end)


Also i’m new with tweens

Vector3’s are immutable so you can’t change individual numbers, you have to change the whole vector.

	if humanoid.MoveDirection.X > 0.01 then
		propertyToTween.Orientation = Vector3.new(0,0,-360)
	elseif humanoid.MoveDirection.X < -0.01 then
		propertyToTween.Orientation = Vector3.new(0,0,360)
	end
end)
1 Like

Still not working

You need to adjust the orientation of the wheels based on the player’s movement direction.

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

local ts = game:GetService("TweenService")
local ti = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, -1)

local propertyToTween = {Orientation = Vector3.new(0, 0, 0)}  -- Reset the initial orientation

local RW = script.Parent.RightWheel

local tween = ts:Create(script.Parent.LeftWheel, ti, propertyToTween)
local tween2 = ts:Create(RW, ti, propertyToTween)

humanoid.Running:Connect(function(speed)
	if speed > 0 then
		-- Calculate the player's movement direction
		local moveDirection = character:GetPrimaryPartCFrame().LookVector

		-- Calculate the angle between the player's movement direction and the default forward direction
		local angle = math.deg(math.atan2(moveDirection.X, moveDirection.Z))

		-- Update the propertyToTween table with the new rotation
		propertyToTween.Orientation = Vector3.new(0, angle, 0)

		-- Update the tweens with the updated propertyToTween table
		tween:UpdateTweenInfo(ti, propertyToTween)
		tween2:UpdateTweenInfo(ti, propertyToTween)

		-- Start the tweens
		tween:Play()
		tween2:Play()
	else
		tween:Pause()
		tween2:Pause()
	end
end)

In this code, we reset the initial orientation to zero (Vector3.new(0, 0, 0) ) and then calculate the player’s movement direction using character:GetPrimaryPartCFrame().LookVector . We then calculate the angle between the movement direction and the default forward direction (0 degrees). The propertyToTween.Orientation is updated with the new rotation, and both tweens are updated with the updated propertyToTween table using the UpdateTweenInfo method. Finally, the tweens are started with tween:Play() and tween2:Play() when the player’s speed is greater than zero.

1 Like

Apologies for the confusion. It seems that I made a mistake in the previous response. There is no built-in method to update the tween info directly. Instead, you’ll need to create new tweens with the updated values.

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

local ts = game:GetService("TweenService")
local ti = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, -1)

local RW = script.Parent.RightWheel

local initialOrientation = Vector3.new(0, 0, 0)
local propertyToTween = {Orientation = initialOrientation}

local tween = ts:Create(script.Parent.LeftWheel, ti, propertyToTween)
local tween2 = ts:Create(RW, ti, propertyToTween)

humanoid.Running:Connect(function(speed)
	if speed > 0 then
		local moveDirection = character:GetPrimaryPartCFrame().LookVector
		local angle = math.deg(math.atan2(moveDirection.X, moveDirection.Z))

		local newOrientation = Vector3.new(0, angle, 0)
		tween:Cancel()
		tween2:Cancel()
		tween = ts:Create(script.Parent.LeftWheel, ti, {Orientation = newOrientation})
		tween2 = ts:Create(RW, ti, {Orientation = newOrientation})

		tween:Play()
		tween2:Play()
	else
		tween:Cancel()
		tween2:Cancel()
	end
end)

Seems to be working but:

I dont really see the error, can you explain it to me?

The wheels are rotating to the side.
I want the wheels to spin, like this:
robloxapp-20230526-2139302.wmv (185.0 KB)

Im sorry but in colombia its really late, i think im gonna send you this and tomorrow when i wake up i check devforum

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

local ts = game:GetService("TweenService")
local ti = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, -1)

local RW = script.Parent.RightWheel

local initialOrientation = Vector3.new(0, 0, 0)
local propertyToTween = {Orientation = initialOrientation}

local tween = ts:Create(script.Parent.LeftWheel, ti, propertyToTween)
local tween2 = ts:Create(RW, ti, propertyToTween)

humanoid.Running:Connect(function(speed)
	if speed > 0 then
		local moveDirection = character:GetPrimaryPartCFrame().LookVector
		local angle = math.deg(math.atan2(-moveDirection.X, -moveDirection.Z))  -- Adjusted angle calculation for spinning to the left

		local newOrientation = Vector3.new(0, angle, 0)
		tween:Cancel()
		tween2:Cancel()
		tween = ts:Create(script.Parent.LeftWheel, ti, {Orientation = newOrientation})
		tween2 = ts:Create(RW, ti, {Orientation = newOrientation})

		tween:Play()
		tween2:Play()
	else
		tween:Cancel()
		tween2:Cancel()
	end
end)

It seems to work but it is being weird:

If the behavior of the wheels is not as expected, there might be some additional factors or constraints in your game that are affecting the desired outcome. Here are a few suggestions to help troubleshoot the issue:

  1. Double-check the wheel objects: Make sure that the LeftWheel and RightWheel objects in your character are oriented correctly and positioned appropriately for the desired rotation.
  2. Adjust the initial orientation: If the initial orientation of the wheels is causing unexpected behavior, you can try modifying the initialOrientation value to a different Vector3 that aligns the wheels properly.
  3. Verify other scripts or constraints: Check if there are any other scripts or components that may be interfering with the wheel rotation. For example, if you have any other scripts affecting the rotation or position of the wheels, they could conflict with the rotation applied by the code we discussed.
  4. Test with simplified code: Create a simple test scenario with only the essential components, such as the character, wheels, and the rotation code. This can help isolate the issue and identify any potential conflicts or inconsistencies.
1 Like

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