Hello
As the title says, Im currently working on a custom character controller. My main goal for this project is making it lightweight.
Currently I;ve managed to run the game with 600 characters, with about 80 percent script usage. My goal it to take that usage down to 40% more less if possible. Please review my code for performace imporvements!
As a side note, server-clinet replication is not a problem as this will be running completely on the client.
local CollectionService = game:GetService("CollectionService")
local PhysicsService = game:GetService("PhysicsService")
PhysicsService:CreateCollisionGroup("NPCharacterGroup")
PhysicsService:CollisionGroupSetCollidable("NPCharacterGroup", "Default", false)
local cm = {}
cm.__index = cm
--Settings
cm.walkSpeed = 8
cm.gravity = 1
cm.tolerance = 0.01
cm.dampFactor = 0
cm.dampOffset = 10
cm.wallFriction = 0.75
cm.maxClimb = 2
cm.regionSearchRange = 2
--Variables
cm.position = Vector3.zero
cm.targetPos = Vector3.zero
cm.onGround = true
cm.touchingWall = false
cm.hitboxRays = {}
cm.hipHeight = 0
cm.rayPara = nil
function cm:update(deltaTime)
local currentPos = self.model.PrimaryPart.Position
local velocity = Vector3.zero
local prevVelocity = self.velocity
--Movement
local diff2D = (self.targetPos - currentPos) * Vector3.new(1, 0, 1)
local diff2DMag = diff2D.Magnitude
if diff2DMag > self.tolerance then
local stepDistance = self.walkSpeed * deltaTime
if diff2DMag > stepDistance then
velocity = diff2D.Unit * stepDistance
else
velocity = diff2D
end
end
--Gravity
if not self.onGround then
velocity += Vector3.new(0, prevVelocity.Y - (self.gravity * deltaTime), 0)
end
--Character Collusion
local inContact = false
local characterPush = Vector3.zero
if not self.touchingWall then
local offset = ((Vector3.xAxis + Vector3.zAxis) * self.regionSearchRange) + (Vector3.yAxis * self.hipHeight)
local min = currentPos + velocity - offset
local max = currentPos + velocity + offset
local region = Region3.new(min, max)
region:ExpandToGrid(4)
local parts = workspace:FindPartsInRegion3(region, self.model)
for _, part in ipairs(parts) do
if part.Name == "hitbox" and CollectionService:HasTag(part.Parent, "NPCharacter") then
inContact = true
local diff = (part.Position - (currentPos + velocity)) * Vector3.new(1, 0, 1)
local overlap = diff.Magnitude - self.hitboxRadius * 2
if overlap < 0 then
characterPush += diff.Unit * overlap / 2
end
end
end
end
if characterPush.Magnitude > self.hitboxRadius then
characterPush = characterPush.Unit * self.hitboxRadius
end
velocity += characterPush
--Object Collusion
local groundCast = workspace:Raycast(currentPos + velocity, Vector3.yAxis * self.hipHeight * -1, self.rayPara)
if groundCast then
local diff = groundCast.Distance - self.hipHeight
if diff > 0 then
self.onGround = false
else
self.onGround = true
--if diff * -1 > self.tolerance then
-- velocity += Vector3.yAxis * diff * -1
--end
local climbHeight = diff * -1 --* math.exp(diff * self.dampFactor)
if climbHeight < self.maxClimb then
velocity += Vector3.yAxis * climbHeight
end
end
else
self.onGround = false
end
local touchingWall = false
for _, vector in ipairs(self.hitboxRays) do
local result = workspace:Raycast(currentPos + velocity, vector, self.rayPara)
if result then
touchingWall = true
local diff = self.hitboxRadius - result.Distance
velocity -= vector * diff * math.exp(-1 * diff * self.dampFactor)
end
end
--wall friction
--if touchingWall then
-- velocity *= self.wallFriction
--end
--tolerance
local filter = Vector3.yAxis
if math.abs(velocity.X) > self.tolerance then
filter += Vector3.xAxis
end
if math.abs(velocity.Z) > self.tolerance then
filter += Vector3.zAxis
end
velocity *= filter
--Update CFrame
self.model:SetPrimaryPartCFrame(CFrame.new(currentPos + velocity))
--Update variables
self.position = currentPos + velocity
self.velocity = velocity
self.touchingWall = touchingWall
--Debug
if touchingWall then
self.model.hitbox.SelectionBox.Color3 = BrickColor.Red().Color
else
self.model.hitbox.SelectionBox.Color3 = BrickColor.Blue().Color
end
end
function cm.new(model)
local new = {}
setmetatable(new, cm)
new.model = model
local hitbox = model.hitbox
new.hitboxRays = {
Vector3.new(hitbox.Size.X / 2, 0, 0),
Vector3.new(-hitbox.Size.X / 2, 0, 0),
Vector3.new(0, 0, hitbox.Size.Z / 2),
Vector3.new(0, 0, -hitbox.Size.Z / 2)
}
new.hitboxRadius = math.max(hitbox.Size.X, hitbox.Size.Z) / 2
new.regionSearchRange = new.hitboxRadius * 2
new.hipHeight = hitbox.Size.Y / 2
new.maxClimb = new.hipHeight / 4
local rayPara = RaycastParams.new()
rayPara.CollisionGroup = "Default"
new.rayPara = rayPara
CollectionService:AddTag(model, "NPCharacter") -- Non Physices based Character
for _, child in ipairs(model:GetChildren()) do
if child:IsA("BasePart") then
PhysicsService:SetPartCollisionGroup(child, "NPCharacterGroup")
end
end
return new
end
return cm
Video
https://drive.google.com/file/d/12Nv0Aw0ZBR5fM4VGOm6VUXZQVAqWaks6/view?usp=sharing