Object rotation on A UI

So the out come that I want is the rotation Y axis of the object/character constant not following the rotation of X axis.

charView.MouseMoved:Connect(function(X, Y)
	if holdInDisplay == false then return end

	if currentX then
		clonedChar.PrimaryPart.CFrame *= CFrame.fromEulerAnglesXYZ(0, math.rad((X-currentX) * 0.5), 0)
	end

	if currentY then
		clonedChar.PrimaryPart.CFrame = clonedChar.PrimaryPart.CFrame:ToWorldSpace(CFrame.Angles(-math.rad((Y-currentY) * 0.5), 0, 0))
	end
	
	currentX = X
	currentY = Y
end)
1 Like

Any different idea’s would be helpful.

1 Like

I believe moving the camera would look a lot cleaner and easier to implement, by having it rotate round a pivot point.

2 Likes

Is that still possible on a worldmodel? If so I’ll try that.

Yes, it is.
Here is the documentation
https://developer.roblox.com/en-us/api-reference/class/WorldModel

1 Like

So I just got back and tried using A camera and nothing happened camera didn’t change anything.
I tried changing the pivot orientation, and just got an error message saying that Rotation cannot be assigned to and check the wiki saying that it’s not notreplicated, meaning that it’s not scriptable.

Are you trying to change the CFrames rotation like cframe.Rotation? if so you must instead change the CFrame using something like cframe = something.

I’m not really sure what to change here. I want the Y axis to be constant not following the direction of the X axis. I tired using ToWorldSpace since that looks the right answer but it’s still following the X axis.

If I remember correctly, I would apply the Y axis first, and then follow soon the X axis when adjusting the Camera CFrame.

2 Likes

Store the Y and X angles separately, then set the CFrame directly every time X and Y changes instead of doing it relative to the current. Also do what @Y_VRN said and apply Y before X:

local pitch = 0
local yaw = 0

charView.MouseMoved:Connect(function(X, Y)
  if holdInDisplay == false then return end
 
  yaw += math.rad(X-currentX) * 0.5
  pitch += math.rad(Y-currentY) * 0.5

  currentX = X
  currentY = Y

  local root = clonedChar.PrimaryPart.CFrame
  root.CFrame = CFrame.Angles(0, yaw, 0) * CFrame.new(pitch, 0, 0) + root.Position
end)

2 Likes

So I manage to get it working, but there is a new problem. The problem is if the camera is behind the character and I move the mouse up and down it goes down and up. Is there like a dot product on roblox check if the camera is behind the character?

charView.MouseMoved:Connect(function(X, Y)
	if holdInDisplay == false then return end
	
	if currentX or currentY then
		camera.CFrame = (camera.CFrame.Rotation * CFrame.Angles(0, -(X-currentX) * 0.01, 0)) * CFrame.new(0, 0, 6.5)
		clonedChar.PrimaryPart.CFrame *= CFrame.Angles(-(Y-currentY) * 0.01, 0, 0)
	end
	
	currentX = X
	currentY = Y
end)

Nvm just changing the lines worked. All I need now is to clamp the Y axis to -45, 45.

charView.MouseMoved:Connect(function(X, Y)
	if holdInDisplay == false then return end
	
	if currentX or currentY then
		camera.CFrame = (camera.CFrame.Rotation * CFrame.Angles(-(Y-currentY) * 0.01, 0, 0)) * CFrame.new(0, 0, 6.5)
		clonedChar.PrimaryPart.CFrame *= CFrame.Angles(0, (X-currentX) * 0.01, 0)
	end
	
	currentX = X
	currentY = Y
end)