Make object move forward while jumping

I’m trying to make my slime move forward as it jumps, however can’t figure out a good way. I originally tried MoveTo, but that wasn’t great, so tried BodyForce and still having no luck.

ezgif.com-gif-maker (19)

function Slime:Follow(closestPlayer)
	-- Make slime jump and move forward
	--self.Humanoid.WalkSpeed = self.Speed
	self.Humanoid.Jump = true
	--self.Humanoid:MoveTo(closestPlayer.PrimaryPart.Position)
	
	local Direction = self.Model.PrimaryPart.CFrame.LookVector * 1000
	
	local BodyForce = Instance.new("BodyForce")
	BodyForce.Force = Direction
	
	BodyForce.Parent = self.Model.PrimaryPart
	
	Debris:AddItem(BodyForce, 0.25)
	
	if self.Humanoid:GetState() == Enum.HumanoidStateType.Freefall then
		self.Humanoid.StateChanged:Wait() -- Wait for slime to hit the ground
	end
	
	-- Stop movement
	--self.Humanoid.WalkSpeed = 0
end

Note, I am doing this on the server, is this good? or should I be handling it all on the client?

1 Like

Use “SetPrimaryPartCFrame” to move the model, you’ll need to make sure the model has a part instance set as the value of its “PrimaryPart” property beforehand.
https://developer.roblox.com/en-us/api-reference/function/Model/SetPrimaryPartCFrame

1 Like

No. This will teleport the model. I don’t want that. I want it to move naturally

1 Like

Oh, you meant the humanoid “MoveTo” function, okay have you tried “Move”?

1 Like

Yes, read my question again please

1 Like

No, I mean “Move()” not “MoveTo()”.

1 Like

Yes, it made no difference

50 char

1 Like

You could try a BodyPosition instance, they are used for more “coordinated” movement.

1 Like

Try ApplyImpulse when the character jumps.

1 Like

Bodyforce is fine the force just needs to be strong enough to overcome gravity + its own mass
Apply impulse is also a great solution, wouldn’t really use bodyposition don’t recommand that

You can also just use animations to make them jump but that means they won’t really be able to go over obstacles, but in a open map where there isn’t many obstacles its a performant solution

As for your question on doing it server or not, You should actually take a look at physics network owner, in summary every object that is physics active (like non anchored parts) often change there network owner, the network owner is the one calculating the physics for that object, so for example a part dropping closely near player will have that drop calculated on the player computer

How this relates to your slime is that typically enemies should be physically owned by the nearest player, that way there is smoothness and no delay for behavior

You can read more about it here, in short the network owner of the slime should be the one that does the physics calculations and run AI behavior (which will change constantly generally speaking)

1 Like

This option never occured to me. Would this be a viable solution, if I were to just rotate the slime to face player, then perform jump? The map would be predominately flat, with no real large hills or obstacles in the way.

My only concern is cause animations are client side, not all players would see, if network owner is only set to closest player?? I’ve never really messed with network ownership and know very little about physics in Roblox, so I might be thinking of that all wrong

1 Like

Yes infact some games (even on the frontpage) use this method, the downside is that there isn’t much randomness to the jump and if you want to make the jump really high its possible that players can still damage the enemy by hitting the invisible rootpart (since that part doesn’t move during animation), also due to the invisible rootpart it can’t go over obstacles Although its possible to have alternative code for going over obstacles. Since the animation isn’t really physics based(sorta) its more efficient and more consistent

As for your concern, I am actually not sure what would happen if a player owns the physics ownership of a object and plays a animation, I would test that out but I believe it will replicate; Since I believe this is how the players character’s function

1 Like
function Slime:Follow(closestPlayer)
	-- Make slime jump and move forward
	--self.Humanoid.WalkSpeed = self.Speed
	self.Humanoid.Jump = true
	--self.Humanoid:MoveTo(closestPlayer.PrimaryPart.Position)
	
	local Direction = (closestPlayer.PricamyPart.Position-self.PrimaryPart.Position).unit*100
	
	local bv = Instance.new("BodyVelocity")
	bv.MaxForce = Vector3.new(150000,150000,150000)
	bv.Velocity = Direction
	game.Debris:AddItem(bv, 0.2)
	
	if self.Humanoid:GetState() == Enum.HumanoidStateType.Freefall then
		self.Humanoid.StateChanged:Wait() -- Wait for slime to hit the ground
	end
	
	-- Stop movement
	--self.Humanoid.WalkSpeed = 0
end
1 Like

Just made a quick test on the animation idea. Problem is it resets the slime back to original position

Axis arrows show the start point, and so slime finishes a few studs in front.


ezgif.com-gif-maker (20)

But ye, for some reason it just goes right back

1 Like

Yeah you just need to set the position of the slime to the position of it when it reaches the end

2 Likes

Unsure how I can do that effectively, as the mesh is a single part (HumanoidRootPart) and am using bones to animate

1 Like

Never messed with bones before so not sure either, I believe R0m made a working jump code doe if you want to try that

Can you just set the rootpart position?

1 Like

If the HRP doesn’t move with animation then doing like

hrp:PivotTo(hrp:GetPivot())

just sets them back anyway

1 Like

Yeah I was wondering if you can just set the rootpart position directly, right before the animation ends

1 Like