Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to rotate parts so that it forms a cube around the player?

Asked by 6 years ago
Edited 6 years ago

Problem: I want these six parts to form a cube but I do not know how to rotate and from what I gather from videos you need to while loop it to rotate? I don't know why that would be a necessity, nonetheless this is my last resort. Anyone mind helping me, somewhere it my code, by pinpointing where the problem is or what is to be exact and give an example? My code is pretty repetitive so.

--Ignore if it is local or server as the game I use this script on merges the two into one as filtering enabled is not on so

Code:

game:GetService("UserInputService").InputBegan:Connect(function(key, proc)
    if key.KeyCode == Enum.KeyCode.C then
        if proc == true then
        else
            print("Barrier")
            local part1 = Instance.new("Part")
            part1.Name = "Wall1"
            part1.BrickColor = BrickColor.new("Electric blue")
            part1.Material = "Glass"
            part1.CanCollide = false
            part1.Parent = model:WaitForChild("Head")
            part1.BottomSurface = "Smooth"
            part1.TopSurface = "Smooth"
            part1.Transparency = 0.4
            part1.Reflectance = 0.3
            part1.Size = Vector3.new(20,20,5)
            part1.Anchored = true
            part1.CFrame = model:WaitForChild("Torso").CFrame:toWorldSpace(CFrame.new(0,7,-6))
            local part2 = Instance.new("Part")
            part2.Name = "Wall2"
            part2.BrickColor = BrickColor.new("Electric blue")
            part2.Material = "Glass"
            part2.CanCollide = false
            part2.Parent = model:WaitForChild("Head")
            part2.BottomSurface = "Smooth"
            part2.TopSurface = "Smooth"
            part2.Transparency = 0.4
            part2.Reflectance = 0.3
            part2.Size = Vector3.new(20,20,5)
            part2.Anchored = true
            part2.CFrame = model:WaitForChild("Torso").CFrame:toWorldSpace(CFrame.new(0,7,6))
            local part3 = Instance.new("Part")
            part3.Name = "Wall3"
            part3.BrickColor = BrickColor.new("Electric blue")
            part3.Material = "Glass"
            part3.CanCollide = false
            part3.Parent = model:WaitForChild("Head")
            part3.BottomSurface = "Smooth"
            part3.TopSurface = "Smooth"
            part3.Transparency = 0.4
            part3.Reflectance = 0.3
            part3.Size = Vector3.new(20,20,5)
            part3.Anchored = true
            part3.CFrame = CFrame.new(part3.Position, Vector3.new(0,90,0))
            part3.CFrame = model:WaitForChild("Torso").CFrame:toWorldSpace(CFrame.new(3,0,-6))
            local part4 = Instance.new("Part")
            part4.Name = "Wall4"
            part4.BrickColor = BrickColor.new("Electric blue")
            part4.Material = "Glass"
            part4.CanCollide = false
            part4.Parent = model:WaitForChild("Head")
            part4.BottomSurface = "Smooth"
            part4.TopSurface = "Smooth"
            part4.Transparency = 0.4
            part4.Reflectance = 0.3
            part4.Size = Vector3.new(20,20,5)
            part4.Anchored = true
            part4.CFrame = CFrame.new(part3.Position, Vector3.new(0,90,0))
            part4.CFrame = model:WaitForChild("Torso").CFrame:toWorldSpace(CFrame.new(3,7,6))
            local part5 = Instance.new("Part")
            part5.Name = "Wall5"
            part5.BrickColor = BrickColor.new("Electric blue")
            part5.Material = "Glass"
            part5.CanCollide = false
            part5.Parent = model:WaitForChild("Head")
            part5.BottomSurface = "Smooth"
            part5.TopSurface = "Smooth"
            part5.Transparency = 0.4
            part5.Reflectance = 0.3
            part5.Size = Vector3.new(20,20,5)
            part5.Anchored = true
            part5.CFrame = CFrame.new(part3.Position) * CFrame.Angles(-90,0,0)
            part5.CFrame = model:WaitForChild("Torso").CFrame:toWorldSpace(CFrame.new(0,8,0))
            local part6 = Instance.new("Part")
            part6.Name = "Wall6"
            part6.BrickColor = BrickColor.new("Electric blue")
            part6.Material = "Glass"
            part6.CanCollide = false
            part6.Parent = model:WaitForChild("Head")
            part6.BottomSurface = "Smooth"
            part6.TopSurface = "Smooth"
            part6.Transparency = 0.4
            part6.Reflectance = 0.3
            part6.Size = Vector3.new(20,20,5)
            part6.Anchored = true
            part6.CFrame = CFrame.new(part3.Position, Vector3.new(-90,0,0))
            part6.CFrame = model:WaitForChild("Torso").CFrame:toWorldSpace(CFrame.new(0,8,0))
        end
    end
end)

1 answer

Log in to vote
0
Answered by
RayCurse 1518 Moderation Voter
6 years ago
Edited 6 years ago

In order to make a cube, you will need to position six parts relative to the coordinate frame of the torso. Positioning the "top" face of the cube would be something like this:

--I am assuming that the size of the cube is 5 studs
local part = part:Clone() --example
local torso = character.Torso --example

local offset = size/2
relativePos = Vector3.new(0 , offset , 0)
local unit = Vector3.new(0 , -1 , 0) --Up vector
part.CFrame = torso.CFrame * CFrame.new(relativePos , relativePos + unit)   
part.Parent = workspace

From here, all you need to do is adjust the relative position and unit vector for different faces. For example, your "bottom" face would just require copying the above code and changing the offset to -size/2 and changing the unit to Vector3.new(0 , 1 , 0). In order to demonstrate, here is a function that will make a cube around an arbitrary coordinate frame:


Function

function createCube (cf , size , part , thickness)
    --Defaults
    cf = cf or CFrame.new()
    size = size or 4
    part = part or Instance.new("Part")
    thickness = thickness or 1

    --Variables
    local offset = size/2 - thickness/2
    local relativePos

    --Adjust the blueprint part size
    part.Size = Vector3.new(size , size , thickness)

    --Face One
    local partOne = part:Clone()
    relativePos = Vector3.new(0 , offset , 0)
    partOne.CFrame = cf * CFrame.new(relativePos , relativePos + Vector3.new(0 , -1 , 0))   
    partOne.Parent = workspace

    --Face Two
    local partTwo = part:Clone()
    relativePos = Vector3.new(offset , 0 , 0)
    partTwo.CFrame = cf * CFrame.new(relativePos , relativePos + Vector3.new(-1 , 0 , 0))   
    partTwo.Parent = workspace

    --Face Three
    local partThree = part:Clone()
    relativePos = Vector3.new(0 , 0 , offset)
    partThree.CFrame = cf * CFrame.new(relativePos , relativePos + Vector3.new(0 , 0 , -1)) 
    partThree.Parent = workspace

    --Face Four
    local partFour = part:Clone()
    relativePos = Vector3.new(0 , -offset , 0)
    partFour.CFrame = cf * CFrame.new(relativePos , relativePos + Vector3.new(0 , 1 , 0))   
    partFour.Parent = workspace

    --Face Five
    local partFive = part:Clone()
    relativePos = Vector3.new(-offset , 0 , 0)
    partFive.CFrame = cf * CFrame.new(relativePos , relativePos + Vector3.new(1 , 0 , 0))   
    partFive.Parent = workspace

    --Face Six
    local partSix = part:Clone()
    relativePos = Vector3.new(0 , 0 , -offset)
    partSix.CFrame = cf * CFrame.new(relativePos , relativePos + Vector3.new(0 , 0 , 1))    
    partSix.Parent = workspace

    --Return parts
    return partOne , partTwo , partThree , partFour , partFive , partSix
end
  • The first parameter is the coordinate frame specifying the center of the cube. Note that the function also takes into account the rotation.

  • The second parameter is the length of the cube in studs.

  • The third parameter is the part that will be used to make the cube. The part will automatically be resized to fit the cube so the only properties of this part that really matter are the colors, materials, anchored, and can collide properties.

  • The last parameter is the thickness of the cube in studs.

  • The function returns all the parts that were used to make the cube. All of the parameters except for the center coordinate frame are optional.

Explanation

The offset variable carries the distance each face must be from the center of the cube. The reason size is being divided by two is because the we only have to go halfway from the center in order to get to a certain face. For every face, we are also defining a variable called relativePosition. This carries the position the face must be in relative to the center of the cube. For example, the relative position Vector3.new(0 , -offset , 0) would be directly below the center of the cube because we are defining it to be lower on the y axis by offset amount of studs. When we are finally setting the coordinate frame of the face we are setting the relative position by multiplying the center coordinate frame and the relative position. The rotation is set by giving another position that would be basically adjacent to the position. For example, RelativePos + Vector3.new(0 , 1 , 0) would be exactly one stud above the RelativePos.

0
Thanks, after reading this I understand a lot more when dealing with rotating parts. I also thank you for going into such great detail, it really helped ;3. yougottols1 420 — 6y
Ad

Answer this question