I’d like to have a part scale based on its surroundings. How would I achieve this? I’m guessing this has to do with raycasting and some math.
Example:
I’d like to have a part scale based on its surroundings. How would I achieve this? I’m guessing this has to do with raycasting and some math.
Example:
Two crude solutions would be:
raycast from each side of the box (you can get the starting directions by using the part’s CFrame.rightVector,lookVector), then the distance of the raycast will give you 1/2 the size.
Keep making box bigger until it intersects then shrink it till it no longer intersects, you can check with the isColliding function or whatever it is called
sorry for the late response.
I ended up firing a raycast on each side, and calculating the center and positioning it correctly. Here’s my code:
local part = script.Parent
local hitPositions = {}
local origin = part.Position
local directions = {
Vector3.new(-10,0,0);
Vector3.new(10,0,0);
Vector3.new(0,0,10);
Vector3.new(0,0,-10);
}
for i,direction in ipairs(directions) do
local result = workspace:Raycast(origin, direction)
local hitPosition = (result and result.Position) or origin + direction
if i == 1 or i == 2 then
table.insert(hitPositions,hitPosition.X)
else
table.insert(hitPositions,hitPosition.Z)
end
end
local sizeX = (hitPositions[2] - hitPositions[1])
local sizeZ = (hitPositions[3] - hitPositions[4])
local posX = (hitPositions[2] + hitPositions[1])/2
local posZ = (hitPositions[3] + hitPositions[4])/2
part.Size = Vector3.new(sizeX,0,sizeZ)
part.Position = Vector3.new(posX,0,posZ)