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)
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)
and when I move right, the MoveDirection.X is 1 (moving right relative to the world space)
You can use this logic in your code to move the wheels left or right depending on the direction the player is moving
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
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)
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)
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)
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.
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)
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)
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:
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.
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.
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.
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.