Weird behavior trying to rotate a model

I am trying to rotate a model by certain increments every time the player presses R or F. However, it’s being really weird and rotating in strange patterns that are not at all what I want the increments to be.

A demonstration is here: https://streamable.com/izaenm

In the output logs, the first number that gets printed (5) is the rotationSize. It’s how much I want the model to rotate by. The player would be able to set this on their own. The second number is math.rad(rotationSize), and the third is math.deg(rotationSize).

Neither of these three numbers work in producing the result I want when combined into the following line of code:

modelToRotate:PivotTo(modelToRotate.PrimaryPart.CFrame * CFrame.Angles(0,math.deg(rotationSize),0))

Any idea what’s going wrong here?

2 Likes

Im on mobile right now so sorry for any formatting

modelToRotate:PivotTo(modelToRotate.PrimaryPart.CFrame * CFrame.Angles(0,math.rad(rotationSize),0))

CFrame.Angles takes in Radians. Just make sure that rotationSize and other variables are correct

1 Like

I’ve already tried using radians. It just rotates in slightly different but equally as janky ways, unfortunately. Never at the increment I actually ask it to.

How is rotationSize being calculated?

Edit: sorry it’s 5

1 Like

A player can use a variable that they can set via a GUI.

By default, it’s 5, but I can change it to anything. Doesn’t work at any increment I set, ultimately.

1 Like

Makes sense

Check the code around the PivotTo? Just cus what we have looks right.

Here’s the full part of the UserInputService code that deal with it:

	local rotationSize = script.Parent.Parent.PlayerData.RotationSize.Value
		
	if input.KeyCode == Enum.KeyCode.R then
		local modelToRotate = script.Parent:FindFirstChildOfClass("Model")
		modelToRotate:PivotTo(modelToRotate.PrimaryPart.CFrame * CFrame.Angles(0,math.rad(rotationSize),0))
		script.Parent.Folder.Orientation.Value = modelToRotate.PrimaryPart.Orientation
	end
	if input.KeyCode == Enum.KeyCode.F then
		local modelToRotate = script.Parent:FindFirstChildOfClass("Model")
		modelToRotate:PivotTo(modelToRotate.PrimaryPart.CFrame * CFrame.Angles(0,math.rad(-rotationSize),0))
		script.Parent.Folder.Orientation.Value = modelToRotate.PrimaryPart.Orientation
	end

The script.Parent.Folder.Orientation.Value thing is just a method for storing the orientation data for later. Not important.

Other than what is seen here there’s nothing else that affects the rotation of the model.

1 Like

The code isn’t running multiple times, right? Maybe check how the UIS event was connected…?

Just as a note, I appreciate the quick responses helping me out.

It’s connected just with a simple line of code
UserInputService.InputBegan:Connect(function(input, Chatting)

And, to that end, I can confirm it doesn’t fire more than once. Changed my print() functions to just do “Fired” when I press a button, and as you can see in this clip it only fires once per press: 2023-09-21 20-50-17

1 Like

There’s nothing wrong with the part you posted, my only other guess is that there’s something weird going on with the model or some other part of the script that’s messing that up.

Could you post the whole script / elaborate what the orientation is for?

This is a system for an object placement system. The only part of the code that adjusts the rotation / orientation of the object is as follows:


--KEYBINDS FOR ROTATIONS, ETC
UserInputService.InputBegan:Connect(function(input, Chatting)
	if Chatting then return end
	if equipped == false then return end
	if valid == false then return end
	
	print("Fired")
	
	local rotationSize = script.Parent.Parent.PlayerData.RotationSize.Value
		
	if input.KeyCode == Enum.KeyCode.R then
		local modelToRotate = script.Parent:FindFirstChildOfClass("Model")
		modelToRotate:PivotTo(modelToRotate.PrimaryPart.CFrame * CFrame.Angles(0,math.rad(rotationSize),0))
		script.Parent.Folder.Orientation.Value = modelToRotate.PrimaryPart.Orientation
	end
	if input.KeyCode == Enum.KeyCode.F then
		local modelToRotate = script.Parent:FindFirstChildOfClass("Model")
		modelToRotate:PivotTo(modelToRotate.PrimaryPart.CFrame * CFrame.Angles(0,math.rad(-rotationSize),0))
		script.Parent.Folder.Orientation.Value = modelToRotate.PrimaryPart.Orientation
	end
end)

Other than what you see here, there is nothing which deals with the orientation of the object. The object’s CFrame changes on RenderStepped based on your mouse position, but that doesn’t touch the rotation and preserves the orientation based on what is registered in the Orientation value I set above:

local roundedPosition = Vector3.new(
				math.floor((Mouse.Hit.Position.X + gridSize / 2) / gridSize) * gridSize,
				Mouse.Hit.Position.Y + script.Parent:FindFirstChildOfClass("Model").PrimaryPart.Size.Y/2,
				math.floor((Mouse.Hit.Position.Z + gridSize / 2) / gridSize) * gridSize
			)
			
			newPreview:PivotTo(CFrame.new(roundedPosition) * CFrame.Angles(script.Parent.Folder.Orientation.Value.X, script.Parent.Folder.Orientation.Value.Y, script.Parent.Folder.Orientation.Value.Z))

The only thing that modifies that Orientation value is the UserInputService section.

Euler angles ~= Orientation
It’s a little weird to explain but there’s a really good post that explains it in detail.

Basically all you need to know from it though is that Orientation (for the most part) is Y, X, Z whilst Euler Angles is X, Y, Z, that and some other odd behavior where it flips sign when it hits 180 degrees and stuff and i’m not qualified enough to even know why it exists.

Anyways, all you need to do is either just supply the angles as euler or use CFrame.fromOrientation().
Or even better, since you are already using values, just save the CFrame as a CFrameValue and apply that.

newPreview:PivotTo(CFrame.new(roundedPosition) * CFrameValue.Rotation))
1 Like

You know, I had come across that article before and tried to use it. I still had the same issue, same with trying to use CFrame.fromOrientation().

But, now, it works. I guess I did something this time that I didn’t do last time. How bizarre.

Thanks though :smiley:

Silly mistakes make learning moments to pay closer attention to things.

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