Hello!
For some reason, my movement script for my npc does not work properly, whenever it chooses a random location and then moves to it, while checking for any obstacles in the way using ray-casting, it still has a chance to hit objects. This is most of the time when goin through narrow paths or hitting a object on the side. Does anyone know how to solve this problem? Here is the full movement script and a video on how he is currently moving. (This is a repost because my other post has not been replied to in a while and seems to not be getting viewed anymore while there is still not a solution to this problem.)
local pathFindingService = game:GetService("PathfindingService")
local maxwell = workspace:WaitForChild("Maxwell")
local humanoidRootPart = maxwell:WaitForChild("HumanoidRootPart")
local hitbox = maxwell:WaitForChild("Hitbox")
local humanoid = maxwell:WaitForChild("MaxwellHumanoid")
local locations = script.Parent
local randomLocations = locations:WaitForChild("randomLocations")
local foodBowl = locations:WaitForChild("foodBowl")
local replicatedStorage = game:GetService("ReplicatedStorage")
local lowHunger = replicatedStorage:WaitForChild("remoteEvents").lowHunger
nomSound = humanoidRootPart:WaitForChild("Nom")
local path = pathFindingService:CreatePath()
local moveRandomly = true
local stop = false
local function moveToDestination(destination)
local path = pathFindingService:CreatePath()
path:ComputeAsync(humanoidRootPart.Position, destination.Position)
local waypoints = path:GetWaypoints()
for i, waypoint in ipairs(waypoints) do
local currentPosition = humanoidRootPart.Position
local direction = (waypoint.Position - currentPosition).Unit
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
raycastParams.FilterDescendantsInstances = {workspace["Urban House"]:GetChildren()}
local raycastResult = workspace:Raycast(currentPosition, direction * (humanoidRootPart.Size.X / 2), raycastParams)
if raycastResult then
humanoid:MoveTo(currentPosition + direction * (raycastResult.Distance - hitbox.Size.X / 2))
else
humanoid:MoveTo(waypoint.Position)
end
wait(0.1)
end
end
local hunger
humanoid.MoveToFinished:Connect(function(randomLocation)
lowHunger.Event:Connect(function(player)
hunger = player:WaitForChild("maxwellStats").Hunger
if foodBowl.foodInBowl.Value == true then
moveRandomly = false
moveToDestination(foodBowl.eatPosition)
end
end)
if moveRandomly == false and stop == false then
nomSound:Play()
hunger.Value = 100
foodBowl.foodInBowl.Value = false
foodBowl.Food.Transparency = 1
moveRandomly = true
wait(math.random(2,5))
end
end)
local proximityPrompt = humanoidRootPart:WaitForChild("ProximityPrompt")
local petActivated = replicatedStorage:WaitForChild("remoteEvents").petActivated
proximityPrompt.Triggered:Connect(function(player)
proximityPrompt.Enabled = false
stop = true
petActivated:FireClient(player)
humanoid:MoveTo(humanoidRootPart.Position)
local maxwellStats = player:WaitForChild("maxwellStats")
local happinessStat = maxwellStats:WaitForChild("Happiness")
if happinessStat.Value <= 65 then
happinessStat.Value = happinessStat.Value + 10
elseif happinessStat.Value < 75 and happinessStat.Value > 65 then
local valueTo100 = 75-happinessStat.Value
happinessStat.Value = happinessStat.Value + valueTo100
end
end)
petActivated.OnServerEvent:Connect(function()
stop = false
local randomLocation = randomLocations:FindFirstChild(math.random(1,10))
moveToDestination(randomLocation)
proximityPrompt.Enabled = true
end)
if moveRandomly == true and stop == false then
while moveRandomly == true and stop == false do
local randomLocation = randomLocations:FindFirstChild(math.random(1,10))
moveToDestination(randomLocation)
wait(math.random(2,5))
end
end