What do you want to achieve?
Hello, I created a script that makes the parts go towards the position of the mouse and I made it so the parts won’t try to stack using raycast and region3. I still don’t know much about a lot of stuff and I am just messing around with the stuff in my script. Anyways, I want the parts to not jitter or try to push other parts to get to the given position, how do I fix this? I’m trying to achieve the Movement System in Cube Eat Cube created by stickmasterluke.
This is what mine looks like -
This is Cube Eat Cube’s movement system -
This is the script -
local Workspace = game:GetService("Workspace")
local Players = game:GetService("Players")
local speed = 10
local mouse = Players.LocalPlayer:GetMouse()
local function bodyPosGyro(part)
local bodyPosition = Instance.new("BodyPosition")
bodyPosition.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
--bodyPosition.D = 5000
--bodyPosition.P = 5000
bodyPosition.Parent = part
local bodyGyro = Instance.new("BodyGyro")
bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)
bodyGyro.P = 1000000
bodyGyro.D = 1000000
bodyGyro.CFrame = CFrame.new(part.Position)
bodyGyro.Parent = part
end
game:GetService("RunService").RenderStepped:Connect(function()
for _, part in pairs(workspace:WaitForChild("Folder"):GetChildren()) do
if part:IsA("BasePart") and part.Name == "Part" then
if not part:FindFirstChild("BodyPosition") and not part:FindFirstChild("BodyGyro") then
bodyPosGyro(part)
end
mouse.TargetFilter = part.Parent
local targetPosition = mouse.Hit.Position
local ray = Ray.new(part.Position, Vector3.new(0, -1, 0) * 10)
local hitPart, hitPosition = workspace:FindPartOnRay(ray, part.Parent)
local currentPosition = part.Position
local direction = Vector3.new(targetPosition.x - currentPosition.x, 0, targetPosition.z - currentPosition.z).unit
local newPosition = currentPosition + direction * speed
if part then
local regionSize = part.Size + Vector3.new(0.1,0.1,0.1)
local region = Region3.new(part.Position - regionSize/2, part.Position + regionSize/2)
--[[local rV = Instance.new("Part")
rV.Name = "rV"
rV.Anchored = true
rV.CanCollide = false
rV.Transparency = 0.5
rV.Size = region.Size
rV.CFrame = region.CFrame
rV.Parent = workspace]]
local collisions = workspace:FindPartsInRegion3(region, part)
for _, collisionPart in pairs(collisions) do
if collisionPart:IsA("BasePart") and collisionPart.Name == "Part" and collisionPart ~= part then
local separationDirection = (part.Position - collisionPart.Position).unit
newPosition = part.Position + separationDirection * speed
end
end
end
if hitPart then
newPosition = Vector3.new(newPosition.x, hitPosition.y, newPosition.z)
end
part:WaitForChild("BodyPosition").Position = newPosition
part:WaitForChild("BodyGyro").CFrame = CFrame.new(Vector3.new(part.Position.x, part.Position.y, part.Position.z))
local distance = (targetPosition - part.Position).magnitude
if distance < 1.5 then
part:WaitForChild("BodyPosition").Position = targetPosition
if hitPart then
targetPosition = hitPosition
end
end
end
end
end)
game.ReplicatedStorage.Part:FireServer()