I’m making a projectile, and I want to make sure if the player clicks outside of the max distance, the projectile should only go the max distance.
I do feel like I have all the puzzle pieces together, but my brain’s feeling a little fried from trying different solutions.
How would I go about making the new position of where the projectile would go if the player clicked farther than the max distance?
local distance = (clonePos - targetPos).Magnitude
if distance > maxRadius then
local direction = (targetPos - clonePos).Unit
local extraDistance = distance - maxRadius
newPos = --what would go here
distance = maxRadius
end
Just incase:
targetPos: mouse.Hit.Position
closePos: starting position of the projectile
maxRadius: max distance the projectile should be able to travel
using RunService delta time you can get projectile to move
local Vector = clonePos - targetPos
local distance = Vector.Magnitude
local direction = Vector.Unit
local Speed = 2 -- units per second
if distance < maxRadius then
local extraDistance = distance - maxRadius -- not sure why is this here
-- if this runs in a heartbeat/renderstepped use its parameter delta time
newPos = Projectile.Position + Speed * direction * dt
distance = maxRadius -- not sure why is this here
else
Projectile:Destroy()
end
other way
local Vector = clonePos - targetPos
local distance = Vector.Magnitude
local direction = Vector.Unit
local Speed = 2 -- units per second
local Start = tick() -- start of projectile moving
-- assuming that ClonePos is constant
-- put code below in a loop
if distance < maxRadius then
newPos = ClonePos + Speed * direction * (tick() - Start)
distance = maxRadius -- not sure why is this here
end
and if everything i wrote above is misinterpreted and you just want to clamp the magnitude?
I already have the movement for the projectile, what I’m looking for is making sure the end position of the projectile isn’t past the max distance set when the player clicks further than the max distance.
elseif Type == "arc" then
local newPos = targetPos
local clonePos = clone.Position
local distance = (clonePos - targetPos).Magnitude
if distance > maxRadius then
local direction = (targetPos - clonePos).Unit
local extraDistance = distance - maxRadius
newPos = --what would go here
distance = maxRadius
end
arc(clone,
clone.Position,
Vector3.new(
(clonePos.X + newPos.X)/2,
(clonePos.Y + newPos.Y)/2 + (maxRadius * (distance/maxRadius))/2,
(clonePos.Z + newPos.Z)/2),
newPos,
distance/maxRadius * length)
end
local function arc(part, p0, p1, p2, travelTime)
local elapsedTime = 0
local connection
connection = game:GetService("RunService").Stepped:Connect(function(time, deltaTime)
elapsedTime = elapsedTime + deltaTime
-- scale by speed factor
local t = elapsedTime / travelTime
local position
if t > 1 then -- reached end
position = p2
connection:Disconnect() -- fire no more
else
position = quadBezier(t, p0, p1, p2)
end
-- use position to place your part or whatever here
part.Position = position
end)
end
-- find distance between each 2 points and sum it up
local function Length(PointsTable)
local Sum = 0
for i = 2, #PointsTable do
Sum = Sum + (PointsTable[i] - PointsTable[i - 1]).Magnitude
end
return Sum
end
local function arc(part, p0, p1, p2, travelTime)
local TraveledPoints = {}
local Start = tick()
local elapsedTime = 0
local connection
connection = game:GetService("RunService").Stepped:Connect(function(time, dt)
-- disconnect stepped function and end the function if traveled distance is more or equal to maxdistance
if Length(TraveledPoints) >= MaxDistance then
connection:Disconnect()
return
end
elapsedTime = tick() - Start
-- scale by speed factor
local t = elapsedTime / travelTime
local position
if t > 1 then -- reached end
position = p2
connection:Disconnect() -- fire no more
else
position = quadBezier(t, p0, p1, p2)
table.insert(TraveledPoints, position)
end
-- use position to place your part or whatever here
part.Position = position
end)
end