So i want my projectile to have a range and i thought of using math.clamp. Im not sure if its possible but if it isnt i could just use raycasts
local Player = game.Players.LocalPlayer
local Character = Player.CharacterAdded:Wait()
local Max = 25
local Min = 1
local Mouse = Player:GetMouse()
local function CreatePart()
local Part = Instance.new("Part")
Part.Anchored = false
Part.CanCollide = false
Part.Size = Vector3.new(1, 1, 1)
Part.Parent = workspace
Part.Position = Character.HumanoidRootPart.Position
return Part
end
Mouse.Button1Down:Connect(function()
local Part = CreatePart()
local Force = (Mouse.Hit.Position - Character.HumanoidRootPart.Position)
local Direction = Force + Vector3.new(0, workspace.Gravity / 2, 0)
Part:ApplyImpulse(Direction * Part.AssemblyMass)
end)
local YourVector = YourVector
local clamped = math.clamp(YourVector.Magnitude, Min, Max)
local result = YourVector.Unit * clamped
So it will be:
local Force = (Mouse.Hit.Position - Character.HumanoidRootPart.Position)
local Clamp = math.clamp(Force.Magnitude, Min, Max)
Force = Force.Unit * Clamp
local Direction = Force + Vector3.new(0, workspace.Gravity / 2, 0)
Part:ApplyImpulse(Direction * Part.AssemblyMass)
You can make something that shoots towards a point by following this tutorial:
A very easy approximate way to limit the range is just to cap the length of the vector:
local targetPosition -- Set to the position of the target (e.g. the mouse's 3D target)
local characterPosition -- Set to the position of the character
local targetDisplacement = targetPosition - characterPosition
local MAX_DISTANCE = 100
-- If the length of the `targetDisplacement` is too long, set the length to `MAX_DISTANCE`
if targetDisplacement > MAX_DISTANCE then
targetDisplacement = targetDisplacement.Unit * MAX_DISTANCE
end
newTargetPosition = characterPosition + targetDisplacement