-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathroombasMove.luau
More file actions
63 lines (48 loc) · 1.74 KB
/
roombasMove.luau
File metadata and controls
63 lines (48 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Components = require(ReplicatedStorage.Shared.components)
local function roombasMove(world)
local targets = {}
for _, model in world:query(Components.Model, Components.Target) do
table.insert(targets, model.PrimaryPart.CFrame.p)
end
for _, _, charge, model in world:query(Components.Roomba, Components.Charge, Components.Model) do
if charge.charge <= 0 then
continue
end
local closestPosition, closestDistance
local currentPosition = model.PrimaryPart.CFrame.p
for _, target in ipairs(targets) do
local distance = (currentPosition - target).magnitude
if not closestPosition or distance < closestDistance then
closestPosition = target
closestDistance = distance
end
end
if closestPosition then
local body = model.Roomba
local force = body:GetMass() * 20
if closestDistance < 4 then
force = 0
end
local lookVector = body.CFrame.LookVector
local desiredLookVector = (closestPosition - currentPosition).unit
force = force * lookVector:Dot(desiredLookVector)
body.VectorForce.Force = Vector3.new(force, 0, 0)
local absoluteAngle = math.atan2(desiredLookVector.Z, desiredLookVector.X)
local roombaAngle = math.atan2(lookVector.Z, lookVector.X)
local angle = math.deg(absoluteAngle - roombaAngle)
angle = angle % 360
angle = (angle + 360) % 360
if angle > 180 then
angle -= 360
end
local angularVelocity = body.AssemblyAngularVelocity
local sign = math.sign(angle)
local motor = math.sqrt(math.abs(angle)) * sign * -1 * 20
local friction = angularVelocity.Y * -12
local torque = body:GetMass() * (motor + friction)
body.Torque.Torque = Vector3.new(0, torque, 0)
end
end
end
return roombasMove