How would I use CFrame to rotate this Light Launching thing?

Hey, so I am trying to make this light launcher have the light rotate this light while having the starting point of the light to be fixed to that one position, (i made hinge for that). Heres a video of it not working: - YouTube

But it doesnt seem to be working as supposed. heres some pictures, to show what I’m trying to do:

image

local hinge = script.Parent.Hinge
local light = script.Parent.EnhancedCone

local hingepos = hinge.Position

while true do 
	for i = 1, 70 do
		light.CFrame = CFrame.new(hingepos) * CFrame.Angles(0,0,math.rad(i)) * CFrame.new(3.5,0,0)
		task.wait()
	end
end

Hello!
I use a translator to write this text
There are a lot of possibilities to implement what you want, but I will give one of the possible ways.
I suggest you generate a new CFrame(position, target), defining it as a rotation parameter. And position it using the usual lerping between the starting position and the generated vector that is located around the starting position.
Example:

local YourObjectForRotating = nil----- The object that should rotate




xpcall(function()
assert(YourObjectForRotating and YourObjectForRotating:IsA("BasePart"),"Fill in the YourObjectForRotating variable with the object you want to rotate.")		
local startVector = YourObjectForRotating.Position - Vector3.new(0,YourObjectForRotating.Size.Y/2,0)
while wait() do
	local wayVector = Vector3.new(math.sin(tick())*YourObjectForRotating.Size.Magnitude,math.cos(tick())* YourObjectForRotating.Size.Magnitude,0)+startVector
	YourObjectForRotating.CFrame = CFrame.fromMatrix(
		startVector:Lerp(wayVector,0.5),
		CFrame.new(startVector,wayVector).XVector,
		CFrame.new(startVector,wayVector).YVector,
		CFrame.new(startVector,wayVector).ZVector
	)*CFrame.Angles(math.rad(90),0,0)
	end
end,function(err)
	warn(err)
end)

You can also upgrade this code by changing the “wayVector” parameter to any Vector3 that you want to rotate the object to, only it must be within these limits [- magnitude of the size;magnitude of the size].But you can use math.clamp, for automatic limit.

Vector3.new(
math.clamp(YourVector3.X,-YourObjectForRotating.Size.Magnitude,
YourObjectForRotating.Size.Magnitude)
, ...Y and Z) 

To change the point around which the object is rotating, you can change the “startVector” parameter.

2 Likes