Forklift rotation malfuction

  1. What is the issue?
    I want to create a Forklift that moves and rotates to given points, however the rotation part isn’t up to part and failes.

In this project I used CFrames, trigonometry and Tweenservice.

local fork = script.Parent:WaitForChild("AutoForkLift")
local Linepoints = script.Parent:WaitForChild("Linepoints")
local TS = game:GetService("TweenService")

wait(5)

local TweenI = TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.In) -- tweeninfo

for i, v in pairs(Linepoints:GetChildren()) do -- for loop for all the checkpoints
	local PlaceVec = Vector3.new(Linepoints:FindFirstChild(tostring(i)).Position.X - fork.PrimaryPart.Position.X, 0, Linepoints:FindFirstChild(tostring(i)).Position.Z - fork.PrimaryPart.Position.Z)
	-- calculates the position it needs to go to
	
	local TweenProp = { -- tween properties
		CFrame = fork.PrimaryPart.CFrame + PlaceVec
	}
	
	local tween = TS:Create(fork.PrimaryPart, TweenI, TweenProp) -- creates the tween

	tween:Play()
	tween.Completed:Wait()
	wait(1)
	-- plays the tween, + extra sec of delay
	-- No problem so far
	
	local CalcRot = math.atan((Linepoints:FindFirstChild(tostring(i+1)).Position.X - fork.PrimaryPart.Position.X)/ (Linepoints:FindFirstChild(tostring(i+1)).Position.Z - fork.PrimaryPart.Position.Z))
	-- calculates the amount of degrees(in radians) it has to rotate from Point A to Point B
    -- It calculates this by useing the inverse of Tan on the Opposite and Adjacent sides
    -- Formula: Tan^-1(O/A)
	
	print(math.deg(CalcRot))
	local TweenProp2 = { -- rotationtweenproperties
		CFrame = fork.PrimaryPart.CFrame * CFrame.Angles(0, CalcRot, 0) -- the problem starts here
	}
	
	local tween2 = TS:Create(fork.PrimaryPart, TweenI, TweenProp2)
	-- creates rotationtween
	
	tween2:Play()
	tween2.Completed:Wait()
	wait(1) -- plays the rotationtween, + extra sec of delay
end

robloxapp-20240914-1752002.wmv (731.6 KB)

As you can see in the video above, the machine just adds the amount of degrees to the rotation of the machine. I don’t know how to come to a solution for this project.

This is likely due to the way you’re using CFrame.Angles to rotate the forklift, which just adds to the current rotation instead of setting the angle relative to the next point.

To fix this, you’ll need to calculate the correct orientation of the forklift based on the direction to the next point and set the rotation directly using CFrame.lookAt.

local fork = script.Parent:WaitForChild("AutoForkLift")
local Linepoints = script.Parent:WaitForChild("Linepoints")
local TS = game:GetService("TweenService")

wait(5)

local TweenI = TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.In) -- tweeninfo

for i, v in pairs(Linepoints:GetChildren()) do -- for loop for all the checkpoints
	local PlaceVec = Vector3.new(Linepoints:FindFirstChild(tostring(i)).Position.X - fork.PrimaryPart.Position.X, 0, Linepoints:FindFirstChild(tostring(i)).Position.Z - fork.PrimaryPart.Position.Z)
	-- calculates the position it needs to go to

	local TweenProp = { -- tween properties
		CFrame = fork.PrimaryPart.CFrame + PlaceVec
	}

	local tween = TS:Create(fork.PrimaryPart, TweenI, TweenProp) -- creates the tween

	tween:Play()
	tween.Completed:Wait()
	wait(1)
	-- plays the tween, + extra sec of delay
	-- No problem so far

	local CalcRot = math.atan((Linepoints:FindFirstChild(tostring(i+1)).Position.X - fork.PrimaryPart.Position.X)/ (Linepoints:FindFirstChild(tostring(i+1)).Position.Z - fork.PrimaryPart.Position.Z))
	-- calculates the amount of degrees(in radians) it has to rotate from Point A to Point B

	print(math.deg(CalcRot))
	local directionToNext = (Linepoints:FindFirstChild(tostring(i + 1)).Position - fork.PrimaryPart.Position).unit
	local TweenProp2 = { -- rotationtweenproperties
		CFrame = CFrame.lookAt(fork.PrimaryPart.Position, fork.PrimaryPart.Position + directionToNext) -- the problem starts here
	}

	local tween2 = TS:Create(fork.PrimaryPart, TweenI, TweenProp2)
	-- creates rotationtween

	tween2:Play()
	tween2.Completed:Wait()
	wait(1) -- plays the rotationtween, + extra sec of delay
end
1 Like

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