Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to make a shark follow a player smoothly?

I was scripting a shark, and I got all the pathfinding stuff done. The problem I was facing was getting the shark to move on the points. I tried using a BodyPosition, but when the in pairs loop reached only the second point (I verified using prints), the shark seemed to disappear. I also tried using a Humanoid and MoveTo, but that just made the shark just move away from the player. (Soon there were over 600 points). The only thing I could do was this.

local allPoints = script.Parent.Parent.PointFolder:GetChildren()

for i, v in pairs(allPoints) do
	script.Parent.Position = v.Position
end

It worked, but it was obviously not what I want. The shark has very jittery movement, and it does not face the place where it's going, though this may be something else. Can anyone explain how to get the BodyPosition or Humanoid to work? Thanks in advance.

1 answer

✓ Accepted answer

What you are probably looking for is a combination of BodyPosition and BodyGyro. BodyPosition can allow you to move it towards the next position, while BodyGyro will rotate it to face towards the next point (however, I'm not sure why the shark disappeared with BodyPosition). If you were to do this, it would be something like the following:

local Shark = script.Parent

local BP = Instance.new("BodyPosition")
BP.MaxForce = Vector3.new(1,1,1)*math.huge
BP.Parent = Shark
local BG = Instance.new("BodyGyro")
BG.MaxTorque = Vector3.new(1,1,1)*math.huge
BG.Parent = Shark

for _,Point in pairs(allPoints) do
	BP.Position = Point.Position
	BG.CFrame = CFrame.new(Shark.Position, Point.Position)
	BP.ReachedTarget:Wait()
end

If there is a delay between moving the position, you can try setting network ownership of the shark to the server.

However, if BodyMovers do not work for you, you are able to use this method to tween the model if it is anchored.

Hope this helps!

3 comments

0

Hey, sorry for the late reply, but the shark still seems to be disappearing. The MeshPart just disappears. Thanks for all the rest, though!

0

I found what I think is the problem with BodyPositions. I tried putting all my scripts etc. in a regular part. The BodyPosition's position did not change, it just stayed at 0, 50, 0, even when I went far into the range of the part. I personally think the shark gets teleported into the void or something. I'm gonna try tweening now. Thanks for all the help!

0

No luck with Tweening either. It just disappears. I'm gonna accept your answer, thanks for the help. Gonna make a different question with the whole script to make it easier to see if I did something wrong elsewhere

This discussion is preserved as it was. New answers, comments, and votes are closed.