Hey guys, my goal here is to make a part that you can drag back and forth, which will return a value between 1 and -1, and will then give propulsion to a vehicle. My issue here though is that I cant figure out the math to reach the value, it never makes it to positive 1 and decides to go back down.
I don’t really know how to explain it, so I will attach a video of what I mean.
https://gyazo.com/646b1e86dfcf053397b3eb1e1d1c02e3
If someone can take a look and provide me some feedback, here is the code.
local user = game.Players.LocalPlayer
local mouse = user:GetMouse()
local isDragging = false
local camera = workspace.CurrentCamera
local params = RaycastParams.new()
params.FilterDescendantsInstances = {}
params.FilterType = Enum.RaycastFilterType.Exclude
local function rayResult(x, y)
local unitRay = camera:ScreenPointToRay(x, y)
return workspace:Raycast(unitRay.Origin, unitRay.Direction * 500, params)
end
local UserInputService = game:GetService("UserInputService")
UserInputService.InputChanged:Connect(function(input)
if isDragging then
local ray = rayResult(mouse.X, mouse.Y)
if ray.Instance and ray.Instance.Parent.Name == "Interactive" and ray.Instance.Type then
local Type = ray.Instance.Type.Value
if Type == "Drag" then
local screenPos = camera:WorldToScreenPoint(ray.Instance.Position)
local screenPosVector2 = Vector2.new(screenPos.X, screenPos.Y)
local mousePosition = UserInputService:GetMouseLocation()
local distance = (screenPosVector2 - mousePosition).Magnitude
local part = ray.Instance
local MIN = -part.Size.Z/2
local MAX = part.Size.Z/2
print(math.clamp(MIN + (MAX - MIN)*(1-(distance/50)), -1, 1))
end
end
end
end)
UserInputService.InputBegan:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
isDragging = true
end
end)
UserInputService.InputEnded:Connect(function(input)
if input.UserInputType == Enum.UserInputType.MouseButton1 then
isDragging = false
end
end)
Thanks!