Hello! I was wondering how to change the position of a part with CFrame but only change the position not the orientation.
Example:
local part = -- the part
part.CFrame = CFrame.new(Vector3.new(0, 0, 0))
When I run that code it changes the position of the part like I want but it also changes the Orientation to 0, 0, 0. How would I make it keep its orientation?
Do note that I am using CFrame because when I change the position of the part the other parts do not go along with it. I am trying to change the position of the HumaonidRootPart by the way.
3 Likes
You only have to use the tweenservice and to change the position. CFrames are only a position + a orientation, so if you only need to change the position simple change thanks a normal Vector3
When I change the position using the part.Position the rest of the body does not go along with it. When I use CFrames though it does.
I still not understand, why you want to change the position of the humanoidrootpart?
If you really need to change these then you not need CFrames as you normally not need to change the position of the player with CFrames (normally)
Try using this:-
local part = -- the part
local rx, ry, rz = part.CFrame:ToOrientation()
part.CFrame = CFrame.new(Vector3.new(0, 0, 0)) * CFrame.fromOrientation(rx, ry, rz)
Note: the variables rx, ry and rz are used to store the orientation component of the CFrame matrix so that we can restore it after changing the CFrame.
2 Likes
You can get the rotational components of the CFrame using:
(CFrame - CFrame.p)
With this, you can change the part’s position and keep it’s orientation if you do this:
local cf = CFrame.new(10, 12, 30) * CFrame.Angles(0, math.pi / 4, 0)
local rot = (cf - cf.p) -- before you change its cframe
part.CFrame = CFrame.new() * rot -- creates a cframe situated at the world center, but rotated 45 degrees on the y-axis
1 Like
Another way you could do it (in case you were wondering/wanted to know):
part.CFrame = part.CFrame + (-part.Position) + Vector3.new(0, 0, 0)
Edit: Oh and if you are actually trying to set the position to 0,0,0, Vector3.new(0, 0, 0) isn’t needed of course (in the example i provided).
1 Like