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

How can i move a part in the direction it's facing?

Asked by
Zzverot 16
5 years ago

So i'm trying to make a part mive in the direction it's facing when it's clicked. In the part there's a clickdetector and this script. the script i am using isn't working. Is it outdated and how can i fix it.

part = script.Parent

function move()
    local Part = script.Parent
    while wait(1) do
        part.CFrame = part.CFrame + part.CFrame.lookVector * 1
end

part.ClickDetector.MouseClick:connect(move)
0
A number or vector multiplied by 1 will always be itself User#24403 69 — 5y
0
@sjr04Alt i know, i'm using / 2 in the new script Zzverot 16 — 5y

1 answer

Log in to vote
1
Answered by 5 years ago

Local/Object Space vs World Space

Local Space or Object space is dependent on the orientation of the part. If the part is rotated, the front of the part will be rotated. In ROBLOX studio, parts that are moved using the move tool are moved in local space automatically. !Local Space World space is like a grid. It never changes. If you move objects in world space, even if it's rotated, it moves the same as if it was never rotated to begin with. !World Space As you can see by the images, the world space is not affected by the rotation of the part.


Moving CFrame by World vs Object Space

In order to move objects in world/object space, you either have to multiply by a cframe or add a vector3. If you multiply by a cframe, it's equivalent to moving in object space. If you add a vector3, it is equivalent to moving in world space. For example, try this script:

local part = Instance.new("Part", workspace)
part.CFrame = CFrame.new(0,10,0) * CFrame.Angles(math.rad(180),0,0)
part.Anchored = true
part.BottomSurface = Enum.SurfaceType.Hinge

while wait(3) do
    part.CFrame = part.CFrame + Vector3.new(0,5)
    wait(3)
    part.CFrame = part.CFrame * CFrame.new(0,5,0)
end

The bottom of the part is a hinge. It has the little yellow cylinder sticking out. You will notice that it is on the top: the part is upside down. The first change we're doing is adding a vector3, or changing it by world space. This means that it basically ignores rotations. Even though the part is upside down, it is moved up. The second operation we do is multiply by a cframe, or moving it in local space. Even though we multiply by positive 5, it moves down because the part is upside down.


Final Product

local part = script.Parent --part only has to be defined once since you aren't changing part anywhere else in your script

function move()
    while wait(1) do --this may cause some problems if people spam click...
        part.CFrame = part.CFrame * CFrame.new(0,0,-1)
    end --you forgot an end here.
end

part.ClickDetector.MouseClick:connect(move)

There are still a few issues you need to fix in your code, but it is mostly finished.



Hope it helps!

0
Does not work for models AngelinaJamie 0 — 3y
Ad

Answer this question