Pathfinding waypoints are making my robot spinning

Hi,

  1. What do you want to achieve?
    I want my robot to look at the waypoint of the pathfindingservice he is moving to.

  2. What is the issue?
    When I make my robot look at the waypoint, it is spinning around randomly.

  3. Extra information:
    The robot is a model and doesn’t have any humanoids or so.

This is my code for moving the robot with pathfindingservice:

local Pathfindingservice = game:GetService("PathfindingService")
local path = Pathfindingservice:CreatePath()
path:ComputeAsync(Robot.PrimaryPart.Position, RandomPlace.Position) -- The path between the robot and a randomly chosen place
local waypoints = path:GetWaypoints()
				
local timeperwaypoint = (30/PlayerStats.RobotSpeed.Value)/#waypoints -- The time between each waypoint
				
for i, waypoint in pairs(waypoints) do
    wait(timeperwaypoint)
    local part = Instance.new("Part")
    part.Size = Vector3.new(0.6,0.6,0.6)
    part.Position = waypoint.Position + Vector3.new(0,2,0)
    part.Anchored = true
    part.CanCollide = false
    part.Parent = game.Workspace
					
	Robot:SetPrimaryPartCFrame(CFrame.lookAt(Robot.PrimaryPart.Position, part.Position)) -- Make the robot look at the waypoint (here is the problem I think)
					
	local TweenService = game:GetService("TweenService")
	local RobotTweenInfo = TweenInfo.new(
	    timeperwaypoint,
		Enum.EasingStyle.Linear,
		Enum.EasingDirection.InOut
	)

	local tweenRobot = TweenService:Create(Robot.PrimaryPart, RobotTweenInfo, {CFrame = part.CFrame}) -- Tween the robot to the waypoint
	tweenRobot:Play()
	part:Destroy()
end

Here you can see what is happening:

Hope you guys can help me!
Thanks!

The reason for the robots rotation is each part your tweening to doesn’t have it’s front face pointing to the final destination. Each iteration of the tween is also rotating the Robot to match the front face of each part which is why it looks like it’s spinning. You will have to either make sure each waypoint parts’ front face is facing the next waypoint position or adjust the cframe during each iteration of the loop:

local TweenService = game:GetService("TweenService")
local RobotTweenInfo = TweenInfo.new(timeperwaypoint, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
local newCF=CFrame.new(part.Position,  RandomPlace.Position) --< ADJUSTING CFRAME
local tweenRobot = TweenService:Create(Robot.PrimaryPart, RobotTweenInfo, {CFrame =newCF}) -- Tween the robot to the waypoint
tweenRobot:Play()
part:Destroy()

The above code adjusts the robot rotation to always be looking towards the EndPoint (RandomPlace.Position). This isn’t really a proper fix as it wouldn’t look correct if there is a bend in the path it’s following but gives you a starting point to fixing your problem.
I’m by no means proficient enough with tweening CFrames so there could be a better solution!

1 Like

if we tween the position, then it should be fine :sweat_smile:

1 Like