Making a Max Distance for a projectile

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

Any help would be appreciated!

8 Likes

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?

local distance = (clonePos - targetPos).Magnitude
distance = math.clamp(distance, 0, maxRadius)
2 Likes

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.

2 Likes

yeah so you just want to clamp the magnitude??

local distance = (clonePos - targetPos).Magnitude
distance = math.clamp(distance, 0, maxRadius)

both are the same

local distance = (clonePos - targetPos).Magnitude
if distance > maxRadius then
	distance = MaxRadius
end
1 Like

well i already have that set, using clamp or not, but what I’m having a difficult time doing is setting the new position

show me code where you set projectile position

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

could you include that arc function

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

its a bezier curve

oh so you want to limit its distance on a bezier curve?

yeah, making it so that the distance between the the target and projectile origin will never go over a certain distance

-- 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
newPos = closePos + direction * maxRadius

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.