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

How can I make a block move in a direction related to the blocks rotation?

Asked by
Xduel 211 Moderation Voter
10 years ago

So lets say I have:

local part = script.Parent
while wait(1) do
    part.CFrame = part.CFrame + Vector3.new(0,0,1)
end

Now I understand this will make the block move on the Z axis. However, I also understand this will make it move on the Z axis **even if the block is rotated**. So in a sense, I would like to know how to make a block move "forward", a direction that supposedly moves a block in the direction of its front surface,  and even when rotated will keep moving in the direction of its front surface. Is there some function I can use? is there a property hidden in the block that would make this work? Please comment if you need a clarification. All answers appreciated, thank you.

~Xduel

0
thanks for asking this question greatneil80 2647 — 5y

3 answers

Log in to vote
0
Answered by 10 years ago

CFrames have a property called lookVector, which basically returns a unit vector in the direction that the brick is facing. So the correct way to write your script would be like this:

local Part = script.Parent
while wait(1) do
    Part.CFrame = Part.CFrame + Part.CFrame.lookVector * 1 --The 1 means that it will offset the Brick by a unit of 1. You can change the 1 to a different number for a different distance of offset. For example, changing 1 to 10 will make the Part move forward 10 studs in the direction that it is facing

Hope this helped!

Note: For more info on CFrames, click here: CFrames

0
What the, since when were you on scripting helpers, I thought you were occupied selling your gun models to stylis studio... greatneil80 2647 — 5y
0
Is their a way to move blocks to their left or right, instead? Eboyo 0 — 4y
Ad
Log in to vote
4
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

Part objects have a CFrame property which is a CFrame value.

A CFrame (coordinate frame) is the combination of a position in space, and an orientation (rotation -- yaw, roll, pitch)


Using the CFrame, we can get different directions like which is forward.


The lookVector property of CFrame is a unit vector in the direction of the part's front face.

Hence,

part.CFrame = part.CFrame + part.CFrame.lookVector

will move part one stud forward (in the direction of its front face).


The other directions (left, up) don't have properties that we can directly access.

To get them, we can use the matrix math that make CFrames work, but I find that less intuitive.

Instead, we can think about it like this:

orientation = part.CFrame - part.Position
-- JUST the rotation - centered at the origin

partUp = orientation:vectorToWorldSpace( Vector3.new( 0, 1, 0 ) )

toWorldSpace refers to changing the (0, 1, 0) from being relative to the part to be relative to the origin (this is exactly the thinking that you had in picking (0, 0, 1) for what corresponds to "front" for a part)



One minor note: You actually have forward backwards. While I don't have a reason for you, ROBLOX uses (0, 0, -1) as the forward direction (not (0,0,1))

Log in to vote
1
Answered by
Xiousa 156
8 years ago

I know that this is a VERY old thread, but a more efficient way than doing lookVector is Part.CFrame = Part.CFrame * CFrame.new(0,0,-1)

Answer this question