Setting a part's LookVector

Bear with me here, I absolutely despise CFrames. I know this is probably pretty basic stuff but let’s just say I’ve tried to avoid putting myself through this, so my understanding of it is limited.

This is a simplified example of my problem in order to avoid unnecessary details. The situation is still the same.

If I want to set a part’s LookVector to its own LookVector (as in nothing should change at all), why is it that this code works perfectly when the Orientation of the part is Vector3.new(0, 0, 0) but not when it’s anything else? (Currently Vector3.new(0, 180, 90))

local Part = workspace.Part

Part.CFrame = CFrame.new(Part.Position, Part.Position + Part.CFrame.LookVector)

Whenever I run it when the Orientation is Vector3.new(0, 180, 90), it turns the Orientation into Vector3.new(0, -180, 0)

Whenever I run it when the Orientation is Vector3.new(0, 0, 0), the Orientation remains as Vector3.new(0, 0, 0).

What can be done to make the orientation stay the same no matter what it rotates to?

It also appears to only be the Z axis that does not get preserved.

What exactly are you trying to do? What you’re saying doesn’t really make sense.

Code runs about 2 seconds in.

When the Orientation is Vector3.new(0, 180, 90)

When the Orientation is Vector3.new(0, 0, 0) (No change)

Should each one not be set to the LookVector it already has? Shouldn’t the first one not have any difference like the second one?

i’m not sure i know what your asking for either (i don’t think so atleast) , Are you trying to “set” the part’s lookvector to something in particular? if you’re just asking why the orientation changes when it is not set to 0,0,0, that’s because you are using the CFrame.new(Pos, LookAt) constructor.

Part.Position + Part.CFrame.LookVector is the position the part is “looking at” (which changes the orientation)

1 Like

Yes, it is supposed to change the orientation, but I thought my code was supposed to change it to what it already is, as in no difference. How can I “change” its LookVector to what it already is? I heard that the CFrame.new(Pos, LookAt) might be deprecated now so this is really all new stuff to me. Is LookAt different from LookVector?

1 Like

sorry for the sightly late reply :upside_down_face: LookAt is not LookVector, LookAt refers to the vector3 position the first position will “look at”, in other words CFrame.new(Pos, LookAt) constructs a new cframe that looks at (points towards) the vector position LookAt, given Pos. As for the orientation remaining the same, you probably could use that constructor to do that, but it would much simpler if you just set the CFrame “normally” . it doesn’t really make sense to make a part “lookat” itself (i don’t think so anyways). You could set the LookVector by deconstructing the CFrame, but i don’t think that would be the solution to your problem. So i think you might actually be asking something else? or i might just be confused… (which is always a possibility)

1 Like

I need to use that constructor for my actual issue. Since it’s not clear enough what I’m exactly trying to get at here, I’ll try my best to explain.

Let’s say we have part A and part B.

Basically the full thing is that part B is supposed to be inserted at a position 2 studs down from part A and always face in the same direction as part A no matter where part A is rotated in the world. The thing is, there are multiple versions of part B that all have different rotations and positions, so each one has its own unique needs in order to face forwards. So what I did was tell it what its LookVector should be for each variation relative to itself, but it won’t listen when it comes to actually setting it to that because of the problem above.

I cannot set the CFrame normally because the script doesn’t know what to set it to, only what direction to face, and this direction is relative to the way part A is facing. Problem is that part B does not have an orientation of 0 on the Z axis so it doesn’t work.

1 Like

I know that this is a very old topic but I still hope this will help
There are actually a lot of ways that you can do this, here are a few:

local angles = Vector3.new(cf1:ToEulerAnglesXYZ())--creates a vector3 in euler angles 
local cf2  = CFrame.Angles(angles.X,angles.Y,angles.Z) --creates a cframe with the same rotation as cf1

Edit: The above was previously incorrect

You can also use CFrame.fromMatrix()

local cf2 = CFrame.fromMatrix(Vector3.new(Xpos,yPos,Zpos),cf1.XVector,cf1.YVector,cf1.ZVector)

or a more simple approach

local cf2 = cf1 - cf1.Position--a cframe with the same rotation as cf1

If you want to try to understand some of the math on this kind of stuff, you can go to this website, I am sure there are others too.

2 Likes