How to change a part's size and position to a start and end?

  1. What do you want to achieve? Keep it simple and clear!

I want to size and position the cloned part to a start (Handle Part position) and end (raycast position). Let me draw this out so you have a better understanding of what I want!


S is start, E is end

  1. What is the issue? Include screenshots / videos if possible!

There is no issue I just want to figure out how I would I do this!

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

The solution that I have tried is:

Effect.Position = raycastResult.Position
Effect.Size = Vector3.new(raycastResult.Position.X+tool.Handle.Position.X, Effect.Size.Y, Effect.Size.Z)

Effect is the part. Here are the results
image
I don’t want it like this but I want it starting from the Handle Part and to the end of the raycast. Sized and Positioned Correctly! I have also searched on google for any help on doing this and I couldn’t find any!

Here is the whole code (server-script):

local TweenService = game:GetService("TweenService")

local tool = script.Parent
local Animation = tool:WaitForChild("GrappleAnim")
local Grapple = tool:WaitForChild("Grapple")

local Configuration = tool:WaitForChild("Configuration")
local MaxRange = Configuration.MaxRange

local tweenInfo = TweenInfo.new(
	1,
	Enum.EasingStyle.Bounce,
	Enum.EasingDirection.Out,
	0,
	false,
	0
)

Grapple.OnServerInvoke = function(player, mouseRay)
	local character = player.Character
	local mouseOrigin = mouseRay.Origin
	local mouseDirection = mouseRay.Direction * MaxRange.Value
	local Params = RaycastParams.new()
	Params.FilterDescendantsInstances = {character}
	Params.FilterType = Enum.RaycastFilterType.Blacklist

	local raycastResult = workspace:Raycast(mouseOrigin, mouseDirection, Params)
	
	if raycastResult then
		if raycastResult.Instance:IsA("Part") then
			local hitPart = raycastResult.Instance
			local Effect = script.Parent.Effect:Clone()
			Effect.Parent = character
			Effect.Position = raycastResult.Position
			Effect.Size = Vector3.new(raycastResult.Position.X+tool.Handle.Position.X, Effect.Size.Y, Effect.Size.Z)
			local tween = TweenService:Create(character.HumanoidRootPart, tweenInfo, {CFrame = CFrame.new(raycastResult.Position + Vector3.new(0,5,0))})
			tween:Play()
			return true, tween
		end
	end
	return false
end

Thanks!

If you have two positions:

local origin = tool.Handle.Position
local target = raycastResult.Position

You can get the distance between them like

local length = (target - origin).Magnitude

That’s the Z size of your effect:

Effect.Size = Vector3.new(Effect.Size.X, Effect.Size.Y, length)

To position it, you can use a handy constructor for CFrames:

-- not quite right!
Effect.CFrame = CFrame.lookAt(origin, target)

This will position the center of the part at the origin. You really want to push it forward by half it’s length:

-- fixed!
Effect.CFrame = CFrame.lookAt(origin, target) * CFrame.new(0, 0, -length / 2)

Note that we set the Z axis to the length because the lookAt function aligns the Z axis, so it makes things line up without having to rotate anything.

I get this now. It doesn’t align with the handle. Its a bit rotated
image

Hmm could you post the updated code?

local origin = tool.Handle.Position
local target = raycastResult.Position
local length = (target - origin).Magnitude
local Effect = script.Parent.Effect:Clone()
Effect.Parent = character
Effect.CFrame = CFrame.lookAt(origin, target) * CFrame.new(0, 0, -length / 2)
Effect.Size = target-origin

Close, but not quite what you need. The Z axis only should be set to length.

Now I get this!


Code:

local origin = tool.Handle.Position
local target = raycastResult.Position
local length = (target - origin).Magnitude
local Effect = script.Parent.Effect:Clone()
Effect.Parent = character
Effect.CFrame = CFrame.lookAt(origin, target) * CFrame.new(0, 0, -length / 2)
Effect.Size = Vector3.new(Effect.Size.X,Effect.Size.Y, length)

Hard to tell exactly what’s going on because I don’t know where your mouse is, but a few suggestions:

  • Make sure the default Effect.Size has small X and Y values. It doesn’t matter what the Z is, but your original code used a different axis for this so your X size might just be large.
  • Make sure the Effect is anchored and non-cancollide

Thanks I fixed it by changing the x to 0.5!

1 Like