Pivot and LookingAt

Hello! I want the cylinder to always face the red cube. How could I do that?
image
When I do it, the cylinder goes crazy (the image is for reference, but in the real cylinder I have the pivot at the top of the cylinder so that it does not move from a base that supports it)

2 Likes

It sounds like you’re trying to make a turret that points at something. Have you searched for solutions before posting?

At first I tried something like this:

Cube.Position = Vector3.new(-62.5, 2.875, 17)
Cube.Position = Vector3.new(Cube.Position.X + rx, Cube.Position.Y, Cube.Position.Z + rz)
Cylinder.CFrame = CFrame.lookAt(Cylinder.Position, Cube.Position)

But apparently the pivot has some influence

I am trying to aim at the face without modifying the position of the object

Cylinder.CFrame = CFrame.new(Vector3.new(Cube.Position.X + rx, Cube.Position.Y, Cube.Position.Z + rz)) * CFrame.lookAt(Cylinder.Position, Cube.Position)

this should help!! ************

uhh rx and ez are not defined…

local rs = game:GetService("RunService")
local xOffset, yOffset, zOffset = 0, 0, 0
local cyl = ...

rs.RenderStepped:Connect(function(dt)
    cyl.CFrame = CFrame.lookAt(cyl.Position, Cube.Position + Vector3.new(xOffset, yOffset, ZOffset)
end)

mess with the offset values; if its looking too high up, reduce yOffset, etc.

It’s more easier than you think, really.

local partA = workspace.PartA --The part that looks
local partB = workspace.PartB --That part that will be looked at

while task.wait(0.05) do
    partA.CFrame = CFrame.lookAt(partA.Position,partB.Position)
end

Also note that you have to use heartbeat instead of renderstepped if you are on server though.

I’d only use RunService’s Heartbeat if the code you run per frame isn’t that big, since it can severely use up memory and performance if so.

I doubt it would use up a enough memory to be noticeable in this situation.

With only one line of code, it won’t.