Limiting distance from one part to another

I am trying to make an ability similar to the one shown in this video:

The issue I am running into it while creating it, I want it so, like the video, the rocks start coming out at the players position, and then start coming out of the ground while still forming a line with each other and not just teleporting to the mouses position.

Here is a video of what I’ve tried already.

As you can see, I’ve got the base of it done, and I tried implementing the limiter but when you move your mouse across the screen it still leaves major gaps.

Script:

--# PARAMETERS
local raycastParameters = RaycastParams.new()
raycastParameters.FilterType = Enum.RaycastFilterType.Exclude
raycastParameters.FilterDescendantsInstances = { workspace:FindFirstChild('Interactables') }
			
--# RAYCAST
local rootRaycast = workspace:Raycast(HumanoidRootPart.Position, Vector3.new(0, -4, 0), raycastParameters)

--# ROCKS
local lastPosition = rootRaycast.Position or HumanoidRootPart.Position
for i = 1, Ability.details['surge_time'], Ability.details['surge_rate'] do
						
	--# SPAWN
	task.spawn(function()
			
		--# RAYCAST
  		local raycast = workspace:Raycast((mouse_position + Vector3.new(0, 1, 0)), Vector3.new(0, -999, 0), raycastParameters)
		if (raycast) then

			--# POSITIONING
			local size = Ability.details['start_size'] * (i * Ability.details['size_factor'])
			local actualPosition = nil
								
			--# DISTANCE
			local distance = (lastPosition - raycast.Position).Magnitude
			local maxDistance = (i^Ability.details['size_factor'])
								
			if (distance > maxDistance) then
									
				actualPosition = lastPosition:Lerp(raycast.Position, (maxDistance / distance))
									
			else
									
				actualPosition = raycast.Position
									
			end
								
			--# ADD
			actualPosition += Vector3.new(0, 2, 0)
								
			--# SET
			lastPosition = raycast.Position
								
			--# (AT THIS POINT AN EVENT IS FIRED THAT CREATES THE PART ON THE CLIENT)
								
			--# AWAIT
			task.wait(Ability.details['surge_rate'])

	end				

end

['surge_time'] = 4,
['surge_rate'] = 0.05,
['start_size'] = Vector3.new(2, 3, 2),
['size_factor'] = 1.25,
1 Like

You could find the mouse position and the character’s torso position, see their difference using Magnitude, and check if it is within the bounds. If it isn’t, you simply don’t activate the ability.

you can make a position that starts at the character position and then move it towards the mouse position so you can reliably control the speed

if you want unlimited speed with no gaps then i think some drawing programs handle this by either drawing lines between the points or making bezier curves between the points for a more natural look

Do you want there to be no gaps whatsoever? If you want this then you could just get the size of the last boulder and spawn another next to it (position + size/(some constant to make sure they’re flush)) in the direction corresponding to the vector facing towards the player’s mouse (the raycast).

This is sort of what I was talking about. I just wanted to make it so that instead of the rocks just spawning where the mouse is, they follow a line that spawn the rocks towards the position the mouse is at if that makes sense?

Before spawning the rock, you can check the magnitude range between the position of the last rock spawned and the current mouse position, then if the magnitude is higher than expected, spawn the rock next to the previous one but in the direction of the mouse position.

Just noticed that this is already what you’re doing… maybe there is an issue within the calcul.
Don’t it should be (LastPosition - CurrentPosition) / MaxDistance or something similar

actualPosition = lastPosition:Lerp(raycast.Position, (maxDistance / distance))

You can try using Mouse.Hit to get mouse hit position, and after it, calculate the distance between the two points