Get Y rotation of a part

So I have the variable

local Rotation = 90

Chuck that number into math.rad, it rotates 90 degrees

math.rad(Rotation)

Now I’m trying to figure out how to get that 90 from the part itself.
I’ve tried

local Rotation = Model.PrimaryPart.Orientation.Y

But that didn’t work.

I have to get that singular number. Can’t be a CFrame value or anything else. Has to just be the single number.

Reason for this being

Model:SetPrimaryPartCFrame(CFrame.new(Pos, Pos + Normal * 15) * CFrame.Angles(-math.rad(90), math.rad(Rotation), 0) * CFrame.new(0, Model.PrimaryPart.Size.Y / 2, 0))

And so rotation has to be the right number.

4 Likes

Try using Orientation.X instead of Y. The way orientation works is that the axis represents the pivot, not the actual direction it is rotating in.

Rotating around the Y axis is like walking around a vertical pole, you’re not rotating so you face upwards or downwards, you’re rotating so you change which horizontal direction you look at.

So, following this logic, both the X and Z axes represent the vertical direction you are looking in, as they are both like poles lying on the ground. This is also why you rotate vertically using the X axis in CFrame.Angles().

In this case, the X axis is what you want to read from since it is what you rotated by.

1 Like

I’m rotating by Y tho?

That’s how my rotation works

This code here is for when I move a part

Model:SetPrimaryPartCFrame(CFrame.new(Pos, Pos + Normal * 15) * CFrame.Angles(-math.rad(90), math.rad(Rotation), 0) * CFrame.new(0, Model.PrimaryPart.Size.Y / 2, 0))

The -math.rad(90) is to rotate the model, so it’s upright (for some reason the model gets rotated without it) and then math.rad(Rotation) is to preserve the models orientation, which is what gets changed

CFrame.Angles(-math.rad(90), math.rad(Rotation), 0)

With -math.rad(90)
com-video-to-gif%20(1)
If I got rid of the -math.rad(90) this is what I get
com-video-to-gif%20(2)

And if you look at the first vid, you can see my main problem. When I move the model, it rotates automatically, it should stay facing the same way when I move it

2 Likes

Ah, damnit, my bad, I’ve become rusty with Roblox’s transformation system :slight_smile: Looks like I’ll need to get myself refreshed on it… I probably won’t be of any help anytime soon then, sorry.

Although… judging by your code it might be a good idea to stop using orientation entirely and just work with a ‘rotation’ variable. Or split up your code better so that you can understand the effects of normals and on it better.

Regardless of my advice, usually when it comes to problems like this, I find it is good to deconstruct everything step by step. Look into different ways of doing things. Sometimes clumping things together can make it difficult.

2 Likes

What do you mean? What exactly does rotation print out when you set it to orientation Y?

It would return like -45, etc. I need numbers between 0 and 360, not between 90 and -90

Not sure, but this could possibly be fixed by getting the object space orientation
Example:

local osOrientation=Model.PrimaryPart.CFrame:vectorToObjectSpace(Model.PrimaryPart.Orientation)

This returns a vector3 value which is the object space orientation, while by default you get the world space rotation.
So basically in this case osOrientation.Y is what you are looking for.
Again, not sure if that will work, but I guess it’s worth trying.

I’m confused. Why do you want to read the rotation off the part when you can read it from the variable?

Cause the rotation by default is 0 in the variable, when I change its rotation it changes the variable. However, when the script reloads, the models rotation stays at say 180 degrees, but the scripts Rotation variable is at 0, so that’s why I need the Rotation variable to get the rotation from the model and not just be a pre set number

Anyway, I just did some testing in studio. This seems to work:

local x, y, z = part.CFrame:ToOrientation()
print(math.deg(y))
-- prints 90, 180, -90, or 0 (when rotating by increments of 90)
1 Like

As stated previously

because my rotation function works like this

local function RotateModel()
	if Rotation == 0 then
		Rotation = 45
	elseif Rotation == 45 then
		Rotation = 90
	elseif Rotation == 90 then
		Rotation = 135
	elseif Rotation == 135 then
		Rotation = 180
	elseif Rotation == 180 then
		Rotation = 225
	elseif Rotation == 225 then
		Rotation = 270
	elseif Rotation == 270 then
		Rotation = 315
	elseif Rotation == 315 then
		Rotation = 0
	end
	
	Model:SetPrimaryPartCFrame(Model.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(45), 0))
end

That’s silly. You’re just adding 45 to whatever the rotation is. There is no reason it can’t work with a negative number. Also, that’s why your Orientation.Y is -45. -45 is the exact same as 315.

Ok then, how do I get it work in this then

local x, y, z = Model.PrimaryPart.CFrame:ToOrientation()

local function RotateModel()
	Model:SetPrimaryPartCFrame(Model.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(45), 0))
end

-- Move the model
local UnitRay  = Camera:ScreenPointToRay(Mouse.X, Mouse.Y, 1)
local NewRay = Ray.new(UnitRay.Origin, UnitRay.Direction * 1000)
local Hit, Pos, Normal = workspace:FindPartOnRayWithIgnoreList(NewRay, {Model, Player.Character})
		
local PosX = math.floor((Pos.X / 2) + 0.5) * 2
local PosY = Pos.Y
local PosZ = math.floor((Pos.Z / 2) + 0.5) * 2
		
local NewPos = Vector3.new(PosX, PosY, PosZ)
Model:SetPrimaryPartCFrame(CFrame.new(NewPos, NewPos + Normal * 15) * CFrame.Angles(-math.rad(90), 0 , 0) * CFrame.new(0, Model.PrimaryPart.Size.Y / 2, 0))

Moving the model still causes its rotation to go back to 0

If I understand what you’re trying to do correctly, this should work. My advice would be to read up a bit more on how CFrames work.

local yRot = Model.PrimaryPart.Orientation.Y --literally the exact same as CFrame:ToOrientation()

local function RotateModel()
	Model:SetPrimaryPartCFrame(Model.PrimaryPart.CFrame * CFrame.Angles(0, math.rad(45), 0))
	yRot = Model.PrimaryPart.Orientation.Y
end

-- Move the model
local UnitRay  = Camera:ScreenPointToRay(Mouse.X, Mouse.Y, 1)
local NewRay = Ray.new(UnitRay.Origin, UnitRay.Direction * 1000)
local Hit, Pos, Normal = workspace:FindPartOnRayWithIgnoreList(NewRay, {Model, Player.Character})
		
local PosX = math.floor((Pos.X / 2) + 0.5) * 2
local PosY = Pos.Y
local PosZ = math.floor((Pos.Z / 2) + 0.5) * 2
		
local NewPos = Vector3.new(PosX, PosY, PosZ)
Model:SetPrimaryPartCFrame(
	CFrame.new(NewPos, NewPos + Normal * 15) *
	CFrame.Angles(-math.rad(90), 0, 0) *
	CFrame.new(0, Model.PrimaryPart.Size.Y / 2, 0) *
	CFrame.Angles(0, math.rad(yRot), 0)
)

Still getting same problem
com-video-to-gif%20(3)

The green button is to move the model, the rotate function is NEVER being fired in this video

As you can see, everytime I move it, it rotates around 90 degrees on it’s own for no reason.

And this is why having Rotation = 0 didn’t work previously, as everytime I click off this model the GUI gets deleted, and thus the script and when I reclick a new gui is added, with new script, and thus new Rotation variable

Can you please post a video of what happens when you rotate the model around the “Y” axis. I have a suspicion on something.

Also, the root of your issue would seem to lie in the fact that you delete the script everytime you get rid of the gui. I have no clue why you decided to do this.

Edit: Also include screenshot of the PrimaryPart selected with rotate tool enabled. (Make sure to be in local rotation mode)

The reason to delete the script is because if there’s multiple models they cant all share the same rotation value, etc.

And even when I rotate and then go to move after it’s been rotated, it just rotates into a different position.
(the rotate button is the left button)
com-video-to-gif%20(4)

Also the selection box is around the primary part

What is the purpose of this?

NewPos + Normal * 15

I’ve just removed it and the line below it and it no longer rotates 90 degrees each time. If it’s to align it to the slope of the surface below it, that’s not how you should be doing it afaik.

Idk, the ray code and all that wasn’t mine, I’ve just integrated it into it

Just tried it and got this

Model:SetPrimaryPartCFrame(
	CFrame.new(NewPos) *
	CFrame.Angles(-math.rad(90), math.rad(YRot), 0) *
	CFrame.new(0, Model.PrimaryPart.Size.Y / 2, 0)
)

com-video-to-gif%20(5)

If I understand you correctly you want to model rotate to the camera?
If so this should work.

Model:SetPrimaryPartCFrame(
CFrame.new(NewPos, NewPos + Normal) * --no reason to do Normal*15, has the same effect as Normal
CFrame.new(0, Model.PrimaryPart.Size.Y / 2, 0) *
CFrame.Angles(0, 0, math.rad(yRot))
)

(Sorry for no tabs, when I press tab it doesn’t want to place it for some odd reason)