How Can I Make A UnderWater NPC?

Guys How Can I Make A Water NPC That Goes Up And Down In Water Terrain, i already tryed make a “Fly” System to Simulate UnderWater, But it Don’t Move…

Script:


local Fish = script.Parent
local Humanoid = Fish.Humanoid
local Primary = Fish.PrimaryPart

local SimplePath = require(game.ServerStorage.SimplePath, {AgentRadius = 10, AgentHeight = 6})
local Path = SimplePath.new(Fish)

local PositionToMove = nil

local BodyPosition = Instance.new("BodyPosition", Primary)
BodyPosition.MaxForce = Vector3.new(0, math.huge, 0)
BodyPosition.P = 10

function FindTarget()
	local MinimalDistance = 150
	local Target = nil

	for i, Player in game.Players:GetPlayers() do
		if Player.Team ~= game.Teams.Entity then
			if Player.Character then
				if Player.Character.PrimaryPart then
					local Distance = (Primary.Position - Player.Character.PrimaryPart.Position).Magnitude
					if Distance < MinimalDistance then
						Target = Player.Character
						MinimalDistance = Distance
					end
				end
			end
		end
	end
	return Target
end


local function MoveRandom()
	if PositionToMove ~= nil then
		BodyPosition.Position = PositionToMove
		Humanoid.WalkToPoint = PositionToMove
	else
		PositionToMove = Vector3.new(Primary.Position.X + math.random(-50, 50), math.clamp(Primary.Position.Y + math.random(-50, 50), 260.983, 639.983), Primary.Position.Z + math.random(-50, 50))
		BodyPosition.Position = PositionToMove
		Humanoid.WalkToPoint = PositionToMove
	end
	Humanoid.MoveToFinished:Connect(function()
		PositionToMove = nil
	end)
end

local function MoveToPlayer(target)
	BodyPosition.Position = target.PrimaryPart.Position
	Humanoid.WalkToPoint = target.PrimaryPart.Position
end

while wait(0.1) do
	local Target = FindTarget()
	if Target then
		MoveToPlayer(Target)
	else
		MoveRandom()
	end
end
1 Like

Your script is using both Humanoid.WalkToPoint and BodyPosition, which may be conflicting, causing the fish not to move properly. In underwater movement, Humanoid movement is not ideal, so you should rely more on BodyPosition and possibly BodyGyro for smooth navigation.

Fixing the Movement Issues:

  1. Remove Humanoid.WalkToPoint – Humanoids are meant for walking on land, not floating in water.
  2. Increase BodyPosition.P – A value of 10 is too weak to move the fish effectively. Try increasing it to at least 5000 or more.
  3. Add BodyGyro for Stability – This helps keep the fish upright.
  4. Use VectorForce for Smooth Movement – Instead of relying on BodyPosition alone, you can apply force-based movement.

Edited Script:

local Fish = script.Parent
local Primary = Fish.PrimaryPart

local SimplePath = require(game.ServerStorage.SimplePath, {AgentRadius = 10, AgentHeight = 6})
local Path = SimplePath.new(Fish)

local PositionToMove = nil

-- Body movers for smooth underwater movement
local BodyPosition = Instance.new("BodyPosition", Primary)
BodyPosition.MaxForce = Vector3.new(1e6, 1e6, 1e6) -- High force for better movement
BodyPosition.P = 5000

local BodyGyro = Instance.new("BodyGyro", Primary)
BodyGyro.MaxTorque = Vector3.new(1e6, 1e6, 1e6) -- Stabilizes rotation
BodyGyro.P = 5000

function FindTarget()
	local MinimalDistance = 150
	local Target = nil

	for _, Player in game.Players:GetPlayers() do
		if Player.Team ~= game.Teams.Entity then
			if Player.Character and Player.Character.PrimaryPart then
				local Distance = (Primary.Position - Player.Character.PrimaryPart.Position).Magnitude
				if Distance < MinimalDistance then
					Target = Player.Character
					MinimalDistance = Distance
				end
			end
		end
	end
	return Target
end

local function MoveRandom()
	PositionToMove = Vector3.new(
		Primary.Position.X + math.random(-50, 50),
		math.clamp(Primary.Position.Y + math.random(-50, 50), 260.983, 639.983),
		Primary.Position.Z + math.random(-50, 50)
	)
	BodyPosition.Position = PositionToMove
end

local function MoveToPlayer(target)
	BodyPosition.Position = target.PrimaryPart.Position
end

while task.wait(1) do
	local Target = FindTarget()
	if Target then
		MoveToPlayer(Target)
	else
		MoveRandom()
	end
end

Key Fixes:

:heavy_check_mark: Removed Humanoid.WalkToPoint – It doesn’t work well underwater.
:heavy_check_mark: Increased BodyPosition.P – Stronger force for movement.
:heavy_check_mark: Added BodyGyro – Keeps the fish upright.
:heavy_check_mark: Removed unnecessary MoveToFinished event – The BodyPosition handles movement smoothly.

This will allow the NPC to move up and down properly in water and chase players when they get close. :rocket:

1 Like

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