-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmovementConfig.lua
More file actions
150 lines (120 loc) · 4.44 KB
/
Copy pathmovementConfig.lua
File metadata and controls
150 lines (120 loc) · 4.44 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
local RunService = game:GetService("RunService");
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local UserInputService = game:GetService("UserInputService");
local movingPart = workspace.player.playerHitbox;
local leftBall = movingPart.leftBall
local rightBall = movingPart.rightBall
local shiftPressed = ReplicatedStorage.LocalBindables:WaitForChild('shiftPressed')
local shiftReleased = ReplicatedStorage.LocalBindables:WaitForChild('shiftReleased')
local shiftPressedBoolean = false
local shiftMultiplier = 1
--game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame = workspace.player.playerHitbox
local remoteEvent = ReplicatedStorage:WaitForChild('SendPos2Server')
-- Measured in studs per second.
local targetVelocity = 24;
local diagonalFix = 1;
local hitboxSize = 1;
local scene = workspace.scene;
local borderDownCap = scene.borderDown.Position.Y
local borderUpCap = scene.borderUp.Position.Y
local borderLeftCap = scene.borderLeft.Position.Z
local borderRightCap = scene.borderRight.Position.Z
local imAtTheEdge = false;
local TweenService = game:GetService("TweenService")
local vectorOffsetLeft = Vector3.new(0,0,3)
local desiredVectorOffset = Vector3.new(0,3,1)
local tweenValue = workspace.player.playerHitbox.tweenValue
local tweenInfo = TweenInfo.new(.10,Enum.EasingStyle.Linear)
local lastFrameTween = Vector3.new(0,0,0)
local tweenForward = TweenService:Create(tweenValue,tweenInfo, { Value = desiredVectorOffset })
local tweenBack = TweenService:Create(tweenValue,tweenInfo, {Value = vectorOffsetLeft})
local last = os.clock()
function shiftTween()
if shiftPressedBoolean == true then
tweenForward:Play()
else
tweenBack:Play()
end
end
shiftPressed.Event:Connect(function()
shiftPressedBoolean = true
shiftMultiplier = .5
end)
shiftReleased.Event:Connect(function()
shiftPressedBoolean = false
shiftMultiplier = 1
lastFrameTween = tweenValue.Value
end)
local keyCodeToDirectionMap = {
[Enum.KeyCode.W] = Vector3.new(0, 1, 0),
[Enum.KeyCode.A] = Vector3.new(0, 0, -1),
[Enum.KeyCode.S] = Vector3.new(0, -1, 0),
[Enum.KeyCode.D] = Vector3.new(0, 0, 1),
};
local keyCodeToIsPressedMap = {
[Enum.KeyCode.W] = false,
[Enum.KeyCode.A] = false,
[Enum.KeyCode.S] = false,
[Enum.KeyCode.D] = false,
};
local function isRelevantInput(input)
return (
input.KeyCode == Enum.KeyCode.W
or input.KeyCode == Enum.KeyCode.A
or input.KeyCode == Enum.KeyCode.S
or input.KeyCode == Enum.KeyCode.D
);
end
local function getDirectionsToProcess()
local directions = {};
for keyCode, isPressed in pairs(keyCodeToIsPressedMap) do
if isPressed then
local direction = keyCodeToDirectionMap[keyCode];
table.insert(directions, direction);
local diagonalChecker = Vector3.new(0,0,0);
for i,value in pairs(directions) do
diagonalChecker = diagonalChecker + value
end
if diagonalChecker.Magnitude > 1.1 and imAtTheEdge == false then
diagonalFix = 0.707
else
diagonalFix = 1
end
end
end
return directions;
end
local function registerKeyPressed(input, gameProcessedEvent)
if gameProcessedEvent or not isRelevantInput(input) then
return;
end
keyCodeToIsPressedMap[input.KeyCode] = true;
end
local function unregisterKeyPressed(input, gameProcessedEvent)
if gameProcessedEvent or not isRelevantInput(input) then
return;
end
keyCodeToIsPressedMap[input.KeyCode] = false;
end
local function managePartMovement(step)
local directions = getDirectionsToProcess();
local targetPosition = movingPart.Position;
for _, direction in ipairs(directions) do
local offset = (targetVelocity * shiftMultiplier * diagonalFix * step) * direction;
targetPosition = targetPosition + offset;
end
movingPart.Position = Vector3.new(targetPosition.X,math.clamp(targetPosition.Y,borderDownCap+hitboxSize,borderUpCap-hitboxSize),math.clamp(targetPosition.Z,borderLeftCap+hitboxSize,borderRightCap-hitboxSize))
leftBall.Position = movingPart.Position + tweenValue.Value
rightBall.Position = movingPart.Position + Vector3.new(0,tweenValue.Value.Y,-tweenValue.Value.Z)
movingPart.body.Position = movingPart.Position
--print(movingPart.Position)
if targetPosition.Magnitude ~= movingPart.Position.Magnitude then
imAtTheEdge = true else
imAtTheEdge = false
end
--remoteEvent:FireServer(movingPart.Position)
end
UserInputService.InputBegan:Connect(registerKeyPressed);
UserInputService.InputEnded:Connect(unregisterKeyPressed);
RunService.Heartbeat:Connect(managePartMovement);
RunService.Heartbeat:Connect(shiftTween);