How to determine if a negative value or a positive value is needed for a part to always move in the correct direction?

Hello everyone, so I have a scripting problem here. I am trying to move a part in the same direction (the arrow shown above in the diagram) for any given rotation of the referenced part, or which I call here as part2. How would I know if I need to use a negative value or a positive value to always have the part move along the same line no matter what the rotation of part2 is? Basically, how would I know which value would allow the part to always move in the same direction as the arrow.

Sometimes if you use the positive value it goes the other way, and the same with the negative value given different rotations of part2, which I don’t want happening. I want the part to always go in the direction of the arrow no matter which rotation part2 is.

Thank you very much.

You could try using the RightVector of the reference part, instead of hard coding the direction:

local Direction = Reference.CFrame.RightVector * 5 

Thank you for your help. But is there a way to get LeftVector, because I don’t see it in the documentation and I need the left side I think.

You can multiply the RightVector by a negative value to make it essentially a “LeftVector”

local Direction = Reference.CFrame.RightVector * -5 

Does it matter what the negative value is or can I just put in any negative number? Thanks for the help.

If you are using it just as a direction (i.e. not an offset by ‘x’ studs where ‘x’ was 5 before) you just need to negate it.

local Direction = -Reference.CFrame.RightVector

Do I write the code like this?

part.CFrame = part2.CFrame * Direction

Thank you.

CFrame * Vector3 will give you a Vector3 world position representing the CFrame’s local position with the Vector3 offset in local space.

If you want the part to be offseted to the left of part2 by ‘dist’ studs you would write this

local dist = 5
local direction = -part2.CFrame.RightVector * dist
part.CFrame = CFrame.new(part2.CFrame * direction)

but this makes part’s rotation (0, 0, 0), so if you want it to rotate with part2 you would do this:

local dist = 5
local direction = -part2.CFrame.RightVector * dist
part.CFrame = part2.CFrame + direction
1 Like

It doesn’t seem to work. I have two parts with opposite angles, one part with 151 degrees and the other 29 degrees. For the 29 degrees part the relative part goes up which is what I wanted, but for the 151 degrees part the relative part goes down for some reason and goes the opposite way.

Thank you for your help so far. I appreciate it a lot.

Oh you know what I messed up the first code example because it’s in local space. It should be:

local dist = 5
local directionLocal = Vector3.new(-dist, 0, 0)
part.CFrame = CFrame.new(part2.CFrame * directionLocal)

I followed your code but now the parts do not share the same rotation as the main part- they just face 90 degrees. Did I do something incorrectly?

Thanks for the help again!