So I’ve looked everywhere to find the solution for this but nothing helped. Basically I have a game where you have bots and they’re controled using VectorForce, sometimes they’ll go crazy and glitch out of the world, though there’s times when they work just fine, I’m gonna post both examples below and the code.
Working properly:
Glitching out:
function control(character, target)
local mass = character:GetMass()
local directionModify = (target.Position-character.Position).Unit
local direction = Vector3.new(directionModify.X, 0, directionModify.Z)
local targetVelocity = direction * speed
local differenceVelocity = targetVelocity - character.Velocity
if (character.Position-target.Position).Magnitude > 50 and target then
print(character.Name, 'is at least 50 studs away from', target.Name)
character.VectorForce.Force = differenceVelocity * mass * acceleration
else
print(character.Name, 'is close to', target.Name)
character.VectorForce.Force = Vector3.new()
end
end
However, I believe you could rewrite the function to get the acceleration directly to plug into the VectorForce (multiplied by the mass of course) without changing how you’re doing it already:
function GetAcceleration(accelDir, prevVelocity, acceleration, maxSpeed, deltaTime)
local projVel = prevVelocity:Dot(accelDir)
local accelVel = acceleration * deltaTime
if projVel + accelVel > maxSpeed then
accelVel = maxSpeed - projVel
end
return accelDir * accelVel / deltaTime
end
The issue wasn’t caused by the loop, it was caused by the formula I used to calculate the acceleration, so using RunService would make no difference in my code.
Ofcourse i know you already solved your problem. im just saying using runservice for physics calculation are faster than while loop. Using RunService will increase the performance.