Recently I’ve been working on a cool tennis game with interesting game mechanics. In order to keep players, I created a bot that you can practice with when there’s nobody in the server to challenge. In order to make sure the AI plays correctly, I used BodyPosition. However, it looks like the bot reacts a little too late, for some reason it’s delayed???
How do I make sure the BodyPosition is not delayed? Here are it’s properties:
This is the script that moves the AI bot:
BP = script.Parent.BodyPosition
P2 = script.Parent.Parent.Parent.Ball
script.Parent.BodyPosition.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
script.Parent.BodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
z = 250.146
while wait() do
if P2.Position.Z > 216.377 then
z = P2.Position.Z
else
z = 250.146
end
BP.position = Vector3.new(P2.Position.X, 83.794, z)
end
I have tried setting the power to 15000 and even higher but despite using a while wait() loop The AI doesn’t even touch the ball. Could it be because of the gravity? Should I use something other than BodyPosition for that? Should I make the NPC taller???
Well, there’s this property called Dampening. The reason you’re getting these achieved effects is, that because BodyPosition is definitely not the best way to go about this. No matter how much you try, this BodyPosition will always dampen itself, (meaning a distance between the part it’s inside & the destination will always come to a slow pause.) That’s why I recommend you use BodyVelocity instead, and calculate where it needs to be in a loop instead, so that way it’s consistent.
To get you started, so that I’m not leaving you on a blank note here, I’d highly suggest putting this in a loop so that we’re not going based on its starting position, and we calculate a velocity to continue to move us to that point while updating the first position, so we’re not going in a linear direction.
Maybe something like this:
local Speed = 50; -- You can choose how fast you want this to be :)
local Final_Velocity = CFrame.new(NPC.PrimaryPart.Position, Target).LookVector*Speed;
BodyVelocity.Velocity = Final_Velocity;
Please do denote the fact this most likely isn’t going to be 100% accurate, but it does serve as an example to possibly get you started
local rs= game:GetService('RunService')
function spam(func)
local conns = {
rs.Heartbeat:Connect(func), rs.RenderStepped:Connect(func), rs.Stepped:Connect(func)
}
local s = false
spawn(function() while wait() and not s do func() end end)
return {Disconnect = function()
s = true
table.foreach(conns, function(i, c) c:Disconnect() end)
end}
end