Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local LocalPlayer = game:GetService("Players").LocalPlayer
- local RunService = game:GetService("RunService")
- local UserInputService = game:GetService("UserInputService")
- local Camera = workspace.CurrentCamera
- local Mouse = LocalPlayer:GetMouse()
- local target, offset
- local antiGravity = 196.2
- function getAngles(origin, aimPos, aimVelocity, speed)
- local tolerance = 1e-5
- local aimVector = CFrame.lookAt(origin, aimPos).LookVector
- local cosAngle = aimVector:Dot(aimVelocity.Unit)
- local v = speed
- local g = workspace.Gravity - antiGravity
- local initialGuess = (aimPos - origin).Magnitude / (speed - (aimVelocity.Magnitude * cosAngle))
- if initialGuess < 0 or initialGuess ~= initialGuess then
- return nil
- end
- local d = aimPos - origin
- local t = initialGuess
- for _ = 1, 100 do
- local rt = math.sqrt((d.X + aimVelocity.X * t)^2 + (d.Z + aimVelocity.Z * t)^2)
- local st = math.sqrt(v^4 - g * (g * rt^2 + 2 * (d.Y + aimVelocity.Y * t) * v^2))
- if st < 0 then
- return nil
- end
- local thetat = math.atan((v^2 - st) / (g * rt))
- local ft = (rt / (v * math.cos(thetat))) - t
- if math.abs(ft) <= tolerance then
- local displacement = (aimPos + (aimVelocity * t)) - origin
- local horizontalAngle = math.atan2(displacement.X, displacement.Z)
- local verticalAngle = thetat
- local xzDisplacement = rt
- return horizontalAngle + math.rad(180), verticalAngle, xzDisplacement, t
- end
- local rPrime = ((d.X + aimVelocity.X * t) * aimVelocity.X + (d.Z + aimVelocity.Z * t) * aimVelocity.Z) / rt
- local sPrime = (-g * (g * rt * rPrime + aimVelocity.Y * v^2)) / st
- local thetaPrime = (-sPrime * rt - (v^2 - st) * rPrime) / (g * rt^3 + (v^2 - st)^2)
- local cost, sint = math.cos(thetat), math.sin(thetat)
- local dft = ((rPrime / (v * cost)) + (rt * sint * thetaPrime) / (v * cost^2)) - 1
- if dft == nil or dft == 0 then
- break
- end
- t = t - ft / dft
- end
- end
- local function changeBulletGravity(input, _gameProcessed)
- if input.KeyCode == Enum.KeyCode.Two then
- antiGravity = 163.5
- return
- end
- if input.KeyCode == Enum.KeyCode.Three then
- antiGravity = 196.2
- return
- end
- end
- local function setTarget(input, _gameProcessed)
- if input.UserInputType ~= Enum.UserInputType.MouseButton3 then
- return
- end
- if not (target and offset) then
- if not (Mouse.Target and Mouse.Hit) then
- return
- end
- target = Mouse.Target
- offset = target.Position - Mouse.Hit.Position
- else
- target = nil
- offset = nil
- end
- end
- local function aimTarget()
- if not (target and target.Parent) then return end
- local origin = LocalPlayer.Character.HumanoidRootPart.Position
- local horizontalAngle, verticalAngle, dist = getAngles(origin, target.Position - offset, target.Velocity, 800)
- if not dist then return end
- local direction = CFrame.fromOrientation(verticalAngle, horizontalAngle, 0).LookVector * dist
- local pos, _ = Camera:WorldToViewportPoint(origin + direction)
- local loc = UserInputService:GetMouseLocation()
- mousemoverel(pos.X - loc.X, pos.Y - loc.Y)
- end
- RunService:BindToRenderStep('AimAt', Enum.RenderPriority.Camera.Value + 1, aimTarget)
- UserInputService.InputBegan:Connect(changeBulletGravity)
- UserInputService.InputBegan:Connect(setTarget)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement