Make my NPC jump when hitting something too tall for it

I am coding an NPC - however I want it to jump over obstacles it cannot walk around/over. I do not want to use pathfinding.

local Delayt = false
game["Run Service"].Heartbeat:Connect(function()
	if Delayt == false then
		Delayt = true
		script.Parent:MoveTo(script.Parent.Parent:WaitForChild('HumanoidRootPart').Position + Vector3.new(math.random(-50, 50), 0, math.random(-50, 50)))
		script.Parent.MoveToFinished:Connect(function()
			wait(math.random(1, 10))
			Delayt = false
		end)
	end
end)
2 Likes

I guess you could raycast 50 studs… see if it hits something and then see how far your NPC has gotta go before it has to jump

Can you script it? I have only ever used raycasting to kill players.
I know how to make them jump, I only need the raycasting, as I am a newbie with it.

2 Likes

I mean yeah but don’t use it cuz I’m only providing an example

Oh and we also have to get the look direction of the random position it’s moving to so we have to store our Vector in a variable (and make it a CFrame so we can access the LookVector)

local Pos = CFrame.new(math.random(-50, 50), 0, math.random(-50, 50))
--[[ code formatting
on mobile is hard so just
pretend your code is above ]]
if DelayT == false then
    -- should be the right calculation
   local Cast = workspace:Raycast(script.Parent.Position, Pos.LookVector * 50)
   local DistanceTilJump
   if Cast then
      DistanceTilJump = math.abs(script.Parent.Position - Cast.Position)
   end
   local Origin = script.Parent.Position
   coroutine.wrap(function()
      while task.wait(0.5) do
         if (script.Parent.Position - Origin).Magnitude == DistanceTilJump then
            script.Parent.Parent.Humanoid:SetState(Enum.HumanoidStateType.Jumping)
         end
      end
   end)()
   script.Parent:MoveTo(script.Parent.Parent:WaitForChild('HumanoidRootPart').Position + Pos.Position)
end

I’m unsure if this line:

workspace:Raycast(script.Parent.Position, Pos.LookVector * 50)

Is the right calculation but hopefully it is

—————-

while task.wait(0.5) do
   if (script.Parent.Position - Origin).Magnitude == DistanceTilJump then
      script.Parent.Parent.Humanoid:SetState(Enum.HumanoidStateType.Jumping)
end

I don’t think this is good but I haven’t really dealt with something like this before. Maybe someone else has a better method?

Overall I’d recommend pathfinding but it’s your game

You realize the script is IN the humanoid, right?
Also please fix this, it made them begin dancing like wild chickens scared in minecraft.

1 Like

I assumed that from this line:

that this script was in another child of the NPC

I wrote this on mobile cuz I’m not at home atm so I couldn’t test it.

1 Like

Actually I don’t think the code even really works. Forget all my code and the explanations. Just refer to my first post

1 Like