Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do you rotate on a single axis?

Here's a quick sample code of what I'm talking about. I'm trying to keep this part constantly rotated with the Camera but only on 1 rotational axis. But it doesn't seem to stay with the Camera. I have been trying so many methods to try and achieve this effect.

-- Services

local Players = game:GetService('Players')
local RunService = game:GetService('RunService')
local Workspace = game:GetService('Workspace')

-- Client

local Client = Players.LocalPlayer
local Character = Client.Character or Client.CharacterAdded:wait()
local RootPart = Character:WaitForChild('HumanoidRootPart')
local CurrentCamera = Workspace.CurrentCamera

-- Variables

local Part = Instance.new('Part')

Part.Anchored = true
Part.CanCollide = false
Part.Locked = true
Part.BottomSurface = Enum.SurfaceType.Smooth
Part.FrontSurface = Enum.SurfaceType.Motor
Part.TopSurface = Enum.SurfaceType.Smooth
Part.Parent = Workspace

-- Functions

local function Update()
	local CameraDirection = CurrentCamera.CFrame.lookVector
	
	Part.CFrame = CFrame.new(RootPart.CFrame.X, RootPart.CFrame.Y + 2.5, RootPart.CFrame.Z) * CFrame.Angles(0, -CameraDirection.X, 0)
end

-- Misc

RunService.RenderStepped:Connect(Update)

Update()

2 answers

✓ Accepted answer

Wow first off I'd just like to give you props for writing up a beautiful question. You provided a minimum test-case so for someone who can answer your question you make it really easy. Bravo!!!! clap clap clap

Right, now on to your question. There are two ways to do this, you can either move directly from the camera itself and maintain its rotation or you can move from some other point (eg. the rootpart) as you have done.

-- offset directly from the camera
i = 0;
local function Update()
	i = i + 1;
	-- move 10 studs forward from camera then rotate
	Part.CFrame = CurrentCamera.CFrame * CFrame.new(0, 0, -10) * CFrame.Angles(0, math.rad(i), 0);
end

Alternatively:

-- Rotate in relation the camera with some starting cframe that isn't a direct offset from camera
i = 0;
local function Update()
	i = i + 1;
	local cf = RootPart.CFrame + Vector3.new(0, 2.5, 0);
	local cRot = game.Workspace.CurrentCamera.CFrame - game.Workspace.CurrentCamera.CFrame.p;
    Part.CFrame = CFrame.new(cf.p) * cRot * CFrame.Angles(0, math.rad(i), 0);
end

4 comments

0

Not necessarily the answer I was looking for but it's okay I ended up being able to figure it out. Thank you so much!

0

What were you looking for? I can edit the answer for future viewers

0

I posted it

0

Ahh gotcha! Thanks!

local function Update()
	local CameraDirection = CurrentCamera.CFrame.lookVector
	local CameraRotation = math.atan2(-CameraDirection.X, -CameraDirection.Z)
	
	Part.CFrame = CFrame.new(RootPart.CFrame.X, RootPart.CFrame.Y + 2.5, RootPart.CFrame.Z) * CFrame.Angles(0, CameraRotation, 0)
end

This discussion is preserved as it was. New answers, comments, and votes are closed.