How to get Orientation of a Part & set it as CFrame of AlignOrientation

When the attacker throws the object, I want it to stay the same orientation as when its thrown so it doesn’t spin when it lands.

script: (room is thrown object)

local x, y, z = room.CFrame:ToOrientation()
			
local AlignOrient = Instance.new("AlignOrientation")
AlignOrient.Attachment0 = room.Main
AlignOrient.Mode = Enum.OrientationAlignmentMode.OneAttachment
AlignOrient.CFrame = CFrame.Angles(x, y, z)
AlignOrient.Parent = room

The AlignOrientation gets created & parented to the room but Its CFrame Orientation is (0,0,0). The output has no errors & the script analysis isn’t saying something is weird.

Test Video:

1 Like

Have you tried toggling the EnableRigidity property?

Im trying to set the orientation, not make it orient faster

Try printing out the result from :ToOrientation(), are they the right angles?

As a side note, the rotation order of :ToOrientation() is applied in YXZ order, but CFrame.Angles() applies rotation in XYZ order, so you should use either CFrame.fromOrientation() or CFrame.fromEulerAnglesYXZ() instead.

no T-T
it says -0, 0, 0 (more letters for letter limit)

When exactly does the align orientation get created? What does that part of the script look like

Right after the room is cloned

This is the throw part of the skill remote OnServerEvent

lawSkillRemote.OnServerEvent:Connect(function(plr)
	if plr.Character == char then
		if char:GetAttribute("CanThrow") == true and char:GetAttribute("Attacked") == false then
			char:SetAttribute("CanThrow", false)
			script.RoomExists.Value = true
			room = ss.Law.Room10:Clone()
			room.Parent = workspace[tostring(plr.UserId)]

			local y, x, z = room.CFrame:ToOrientation()
			print(y, x, z)

			local AlignOrient = Instance.new("AlignOrientation")
			AlignOrient.Attachment0 = room.Main
			AlignOrient.Mode = Enum.OrientationAlignmentMode.OneAttachment
			AlignOrient.CFrame = CFrame.fromOrientation(y, x, z)
			AlignOrient.Parent = room
			
			room:SetNetworkOwner(plr)
			script.Parent:SetAttribute("RoomThrown", true)
			
			-- anchor room (hit obiect)
			local roomStopped = false
			task.spawn(function()
				repeat
					for i,v in workspace:GetPartBoundsInBox(room.Center.CFrame, Vector3.new(1.5,1.5,1.5), OverlapParams.new()) do
						if v.Name == "Baseplate" then
							
							local liv = Instance.new("LinearVelocity")
							liv.MaxForce = math.huge
							liv.VectorVelocity = Vector3.new(0,0,0)
							liv.Attachment0 = room.Main
							liv.Parent = room
							
							roomStopped = true
						end
					end
					task.wait()
				until roomStopped == true
			end)
			
			-- anchor room (hit ground)
			local connection
			connection = runService.Heartbeat:Connect(function(deltaTime)
				if roomStopped == true then connection:Disconnect() end
				
				if room.Center.Position.Y <= 2 then
					
					local liv = Instance.new("LinearVelocity")
					liv.MaxForce = math.huge
					liv.VectorVelocity = Vector3.new(0,0,0)
					liv.Attachment0 = room.Main
					liv.Parent = room
					
					roomStopped = true
					connection:Disconnect()
				end
			end)

			-- hitbox overlap params
			local params = OverlapParams.new()
			params.FilterDescendantsInstances = {char.Hitbox}
			params.FilterType = Enum.RaycastFilterType.Exclude
			repeat
				for i,v in workspace:GetPartBoundsInRadius(room.Position, 3.3, params) do
					if v.Name == "Hitbox" then
						params:AddToFilter(v)
						attackedB:Fire(v.Parent, plr.UserId, 5)
						knockbackB:Fire()	-- TODO knockback
					end
				end
				task.wait()
			until roomStopped == true 
			
			-- get big
			for i = 1, 13 do
				room.Size += Vector3.new(1.5,1.5,1.5)
				task.wait()
			end
			
			room.Main.Slash1.Enabled = true
			char:SetAttribute("CanTeleport", true)

ss is ServerStorage aaaaaaaaaaaaaaa

the result is the same

Is the CFrame of the room being set before you get its orientation? It seems to me that you create it and then immediately get the orientation before it can rotate, causing an angle of 0, 0, 0

I put this after adding it to workspace:
task.wait()
print(room.CFrame.Rotation)

Output:
0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1

I dont think its if the orientation exists or not. I replaced it with this, which uses the attacker’s HumanoidRootPart instead:

room = ss.Law.Room10:Clone()
			room.Parent = workspace[tostring(plr.UserId)]
			task.wait()
			print(room.CFrame.Rotation)
			local y, x, z = char.HumanoidRootPart.CFrame:ToOrientation()
			print(y, x, z)

			local AlignOrient = Instance.new("AlignOrientation")
			AlignOrient.Attachment0 = room.Main
			AlignOrient.Mode = Enum.OrientationAlignmentMode.OneAttachment
			AlignOrient.CFrame = CFrame.fromOrientation(y, x, z)
			AlignOrient.Parent = room

I thought it was working but it messed up about 10 tries later

Since it’s giving you an angle, try enabling rigidity on the align orientation.

Also, small tangent regarding rotation order

Rotation order isn’t the return/parameter order of the function, it’s the order in which each axis is rotated. In the case of YXZ, first the Y rotation is applied, then the X rotation , and then the Z rotation. Each axis is influenced by the last.

I will apply a rotation of (45, 30, 0) to this red part in the YXZ rotation order.


Notice how rotating 30 degrees on the Y axis (green) affects the X axis (red)

Now if I apply that rotation in XYZ order to this green part…



You can see how the Y axis is influenced by the X rotation in this case, as X rotation is applied first.

This obviously isn’t the main problem but I felt it was important to mention.
This will apply the correct orientation

local x, y, z = char.HumanoidRootPart.CFrame:ToOrientation()
local cframe = CFrame.fromOrientation(x, y, z)
1 Like

RigidityEnabled is working :sweat_smile:

I thought it wasn’t working because the character (which faces the same direction as the room when teleporting) was facing the wrong direction most of the time. The wall was pushing the character.

Tysm for the help! I learned a lot from the replies :smiley:

2 Likes

As @nkmisoup mentioned, you should use CFrame.fromOrientation() instead, but the reason I am writing this reply is because you don’t even need to use Orientation.

You can simply do AlignOrient.CFrame = room.CFrame, and the AlignOrientation will take the orientation of the room’s CFrame (at that time). Note that AlignOrientation ignores the position of the CFrame, it only uses the Rotation

Using CFrames directly can avoid Euler Angle issues: Gimbal lock - Wikipedia

The object spinning is not caused by the AlignOrientation’s CFrame being wrong. It would have to be updated each frame to get it to spin, as AlignOrientation has 1 static target orientation. My guess is that the issue is either the force of the AlignOrientation is too low, or it is not properly set, and thus not acting upon the object

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.