-
Notifications
You must be signed in to change notification settings - Fork 0
/
slingshot
132 lines (87 loc) · 2.74 KB
/
slingshot
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
Tool = script.Parent
local MouseLoc = Tool:WaitForChild("MouseLoc")
VELOCITY = 85 -- constant
local Pellet = Instance.new("Part")
Pellet.Locked = true
Pellet.BackSurface = 0
Pellet.BottomSurface = 0
Pellet.FrontSurface = 0
Pellet.LeftSurface = 0
Pellet.RightSurface = 0
Pellet.TopSurface = 0
Pellet.Shape = 0
Pellet.Size = Vector3.new(1,1,1)
Pellet.BrickColor = BrickColor.new(21)
script.Parent.PelletScript:Clone().Parent = Pellet
function fire(mouse_pos)
Tool.Handle.SlingshotSound:Play()
-- find player's head pos
local vCharacter = Tool.Parent
local vPlayer = game.Players:GetPlayerFromCharacter(vCharacter)
local head = vCharacter:FindFirstChild("Head")
if not head then return end
local dir = mouse_pos - head.Position
dir = computeDirection(dir)
local launch = head.Position + 5 * dir
local delta = mouse_pos - launch
local dy = delta.y
local new_delta = Vector3.new(delta.x, 0, delta.z)
delta = new_delta
local dx = delta.magnitude
local unit_delta = delta.unit
-- acceleration due to gravity in RBX units
local g = (-9.81 * 20)
local theta = computeLaunchAngle( dx, dy, g)
local vy = math.sin(theta)
local xz = math.cos(theta)
local vx = unit_delta.x * xz
local vz = unit_delta.z * xz
local missile = Pellet:Clone()
missile.Position = launch
missile.Velocity = Vector3.new(vx,vy,vz) * VELOCITY
missile.PelletScript.Disabled = false
local creator_tag = Instance.new("ObjectValue")
creator_tag.Value = vPlayer
creator_tag.Name = "creator"
creator_tag.Parent = missile
missile.Parent = workspace
end
function computeLaunchAngle(dx,dy,grav)
-- arcane
-- http://en.wikipedia.org/wiki/Trajectory_of_a_projectile
local g = math.abs(grav)
local inRoot = (VELOCITY*VELOCITY*VELOCITY*VELOCITY) - (g * ((g*dx*dx) + (2*dy*VELOCITY*VELOCITY)))
if inRoot <= 0 then
return .25 * math.pi
end
local root = math.sqrt(inRoot)
local inATan1 = ((VELOCITY*VELOCITY) + root) / (g*dx)
local inATan2 = ((VELOCITY*VELOCITY) - root) / (g*dx)
local answer1 = math.atan(inATan1)
local answer2 = math.atan(inATan2)
if answer1 < answer2 then return answer1 end
return answer2
end
function computeDirection(vec)
local lenSquared = vec.magnitude * vec.magnitude
local invSqrt = 1 / math.sqrt(lenSquared)
return Vector3.new(vec.x * invSqrt, vec.y * invSqrt, vec.z * invSqrt)
end
Tool.Enabled = true
function onActivated()
if not Tool.Enabled then
return
end
Tool.Enabled = false
local character = Tool.Parent;
local humanoid = character.Humanoid
if not humanoid then
print("Humanoid not found")
return
end
local targetPos = MouseLoc:InvokeClient(game:GetService("Players"):GetPlayerFromCharacter(character))
fire(targetPos)
wait(.2)
Tool.Enabled = true
end
script.Parent.Activated:Connect(onActivated)