Change Size of a Part without changing the position of it

  1. What do you want to achieve?
    I’ve got a part that is rotated, and i need to change the length of it without actually moving the part.

  2. What is the issue?
    It would be pretty simple if it wasn’t rotated as I could just add size/2 to the position. But I’ve got no clue how to do that when the part is rotated.

If it’s not clear I could provide a video.

Thank you.

2 Likes

Not sure I understand.

To maintain a part’s position while changing its size, set its CFrame to a cache:

local function updateSize()
local oldCF = part.CFrame

part.Size += Vector3.new(1, 0, 0)
part.CFrame = oldCF
end

I’m guessing you want the end of the Part to stay put, not the center of the part which is what normally happens.

If ds is the change in size as a Vector3 then it’s going to be in local coordinates relative to the part, i.e. if you add 1 on the X axis the part will get bigger on the left/right axis no matter how it’s rotated, and you’ll want to move it by some change in position dp. Like you said, that change in position is half of the change in size, so dp = ds / 2. The Position property of a Part is in world coordinates though, so you can't use dp` directly, you must convert it to world coordinates. You can do that with the VectorToWorldSpace CFrame method. E.g.:

function changePartSizeBy(part, sizeChange)
    part.Size += sizeChange
    part.Position += part.CFrame:VectorToWorldSpace(sizeChange / 2)
end 

I’m pretty sure that will work

1 Like

Well, it still doesn’t work for some reason.

This is my code

	local rng = math.random(3,8)
	
	--Sets the rotation of the Vector3 stored in the EyeRot array.
	leftEye.Orientation = leftEyeRot[rng]
	--rightEye.Orientation = rightEyeRot[rng]
	
	--Makes the EyeLaser part welded into the Eye parent part (sort of like a weld.) offset = leftEye.CFrame:Inverse() * leftEyeLaser.CFrame
	leftEyeLaser.CFrame = leftEye.CFrame * leftEyeOffset 
	--Changes the lenght of the laser to a Vec3 value from an array. (62.75, 0.2, 0.2)
	leftEyeLaser.Size = laserSize[rng]
	
	leftEyeLaser.Position += leftEyeLaser.CFrame:VectorToWorldSpace(laserSize[rng]/2) 

image
Part is the Laser.

I’m probably doing something wrong.

1 Like
local part = Instance.new("Part")
part.Anchored = true
part.Parent = workspace
local oldPos = part.CFrame
part.Size = Vector3.new(10, 10, 10)
part.CFrame = oldPos

This is incredibly simple to achieve, just store the original position (represented by a CFrame value in this case) inside a variable and reassign it to the “CFrame” property of the BasePart instance after its size has been changed.

leftEyeLaser.Size = laserSize[rng]
leftEyeLaser.Position += leftEyeLaser.CFrame:VectorToWorldSpace(laserSize[rng]/2) 

You’re increasing the position by half the total size… not the change in size! Try

local prevSize = leftEyeLaser.Size
leftEyeLaser.Size = laserSize[rng]
local deltaSize = leftEyeLaser.Size - prevSize
leftEyeLaser.Position += leftEyeLaser.CFrame:VectorToWorldSpace(deltaSize/2) 

Still doesn’t work

	local leftEyeOffset = leftEye.CFrame:Inverse() * leftEyeLaser.CFrame
	local rng = math.random(3,8)
	
	leftEye.Orientation = leftEyeRot[rng]

	leftEyeLaser.CFrame = leftEye.CFrame * leftEyeOffset 
	
	local prevSize = leftEyeLaser.Size


	leftEyeLaser.Size = laserSize[rng]
	local deltaSize = leftEyeLaser.Size - prevSize
	
	leftEyeLaser.Position += leftEyeLaser.CFrame:VectorToWorldSpace(deltaSize/2) 

Maybe its because of the offset?

BasePart:Resize() is a thing

https://developer.roblox.com/en-us/api-reference/function/BasePart/Resize

Other thing u can do is

local function Resizepart(part,Size)
    part.Size = Size
    part.CFrame *= CFrame.new(0,0,-Size.Z/2)
end

:Resize doesn’t change the Size at all for some reason

leftEyeLaser:Resize(Enum.NormalId.Left, laserSize[rng].X)

And this has a very similar result as i shown in the video reply above

	part.Size = Size
	part.CFrame *= CFrame.new(-Size.X/2,0,0)

I’m clueless right now :confused:

u are using the wrong Axes for CFrame. Z is the one u are looking for

This is the whole script.

local laserSize = {	
	Vector3.new(0,0,0),  
	Vector3.new(48.8, 0.2, 0.2), --Pillar 1
	Vector3.new(62.75, 0.2, 0.2),
	Vector3.new(79.351, 0.2, 0.2),
	Vector3.new(93.65, 0.2, 0.2),
	Vector3.new(116.55, 0.2, 0.2),
	Vector3.new(154.05, 0.2, 0.2),
	Vector3.new(154.05, 0.2, 0.2)-- Pillar 7
}

local rightEyeRot = {	
	Vector3.new(0,0,0),  
	Vector3.new(-68.26, 59.27, 144.84), --Pillar 1
	Vector3.new(-47.73, 58.37, 166.82),
	Vector3.new(-54.09, 65.47, 153.54),
	Vector3.new(-66.92, 55.09, 150.28),
	Vector3.new(-66.92, 55.09, 150.28),
	Vector3.new(-73.98, 74.48, 131.95),
	Vector3.new(-75.93, 85.72, 118.59) -- Pillar 7
}

local leftEyeRot = {	
	Vector3.new(0,0,0),  
	Vector3.new(-74.76, -16.21, -173.42), --Pillar 1
	Vector3.new(-63.71, -27.15, -160.53),
	Vector3.new(-73.02, -40.65, -145.43),
	Vector3.new(-78.6, -67.54, -122.3),
	Vector3.new(-78.6, -67.54, -122.3),
	Vector3.new(-79.78, -104.99, -79.46),
	Vector3.new(-78.11, -126.98, -57.87) -- Pillar 7
}



local Model = script.Parent
local leftEye = Model:WaitForChild("leftEye")
local leftEyeLaser = leftEye:WaitForChild("Part")
local ClickDetector = Model:WaitForChild("CD"):WaitForChild("ClickDetector")


ClickDetector.MouseClick:Connect(function(player)
	
	local leftEyeOffset = leftEye.CFrame:Inverse() * leftEyeLaser.CFrame
	local rng = math.random(3,8)
	
	leftEye.Orientation = leftEyeRot[rng]

	leftEyeLaser.CFrame = leftEye.CFrame * leftEyeOffset 
	
	leftEyeLaser:Resize(Enum.NormalId.Left, laserSize[rng].X)

end)

the Z’s are 0.2 everywhere though, or?

What I’m trying to achieve is basically a statue that shoots lasers on other parts, pillars in this case.