Problem with rotation and cframe.lookat

I’m trying to make cframe.lookat work with welds and that works fine but now the issue is if an object its welded to moves it doesnt like “auto-register” it and do the math with it


while task.wait() do
	local pos = script.Parent:GetPivot().Position 
	local weld = script.Parent.Parent.MainBody.TurretBody
	local rx, ry = CFrame.lookAt(pos, workspace.Part.Position):ToOrientation()
	weld.C0 = CFrame.new(weld.C0.Position) * CFrame.fromOrientation(0, ry - math.pi, 0)
end

4 Likes

The position you look at must be in the part’s space:

local cf = CFrame.lookAt(workspace.Part.Position, workspace.Part.CFrame:PointToObjectSpace(pos))
weld.C0 = cf - cf.Position

I don’t quite understand where to replace it.
Like this?

	local cf = CFrame.lookAt(workspace.Part.Position, workspace.Part.CFrame:PointToObjectSpace(pos))
	weld.C0 = cf - cf.Position

image

1 Like

It replaces the last two lines in your while loop.

1 Like


Doesnt work too well. Not sure but from experience i dont think tanks look downwards and trough their body

1 Like

Bumping because i still need help

1 Like
local cf = CFrame.lookAt(Vector3.zero, workspace.Part.CFrame:PointToObjectSpace(pos))
weld.C0 = cf - cf.Position

my bad

Nope.

1 Like

I dont know then.
123123123123123

1 Like

I can send you the gamefile so you can work with it if you want.

1 Like

I think you can offset from C0
Unless you replace it with an Attachment and use WorldCFrame

local Weld=WeldObject
local Offset=Vector3.new(x,y,z)

local C0_CFramr=PartWeldIsAttacgedTo.CFrame*C0
local Lookatposition=Cframe.new(Position)
local Inverse=C0_CFrame:ToObjectSpace(Lookatposition)
Weld.C1=Cframe.new(Offset,Inverse.Position)

I created this on my phone, and if you have any questions please ask them.

I dont understand a large chunk of your code. i put it to mine and its just an helicopter

while task.wait() do
	local Weld=script.Parent.Parent.MainBody.TurretBody
	local Offset=Vector3.new(0,0,0)

	local C0_Position=script.Parent.CFrame*Weld.C0
	local Lookatposition=CFrame.new(workspace.Part.Position)
	local Inverse=C0_Position:ToObjectSpace(Lookatposition)
	Weld.C1= CFrame.new(Offset,Inverse.Position)
end

Well first I think you might if misunderstood C0_Position
It looks like you might of attached it to the The actual part the Weld Is on, But I meant the Part that is not moved By the Weld and it attached to

So this?

	local C0_Position= script.Parent.Parent.MainBody.CFrame*Weld.C0

I supposed so.

But I do not think really any one of the people who you are talking too doesnt know the parts of the tank very well.
So I am taking A guess by the name of it.

Turretbody is the weld
Mainbody is what the turret is attached to (turret is the part that moves)
script.parent is wha ti want to move (turret)
weld: c0 = mainbody c1 = script.parent.
I don’t understand any of your code because im not that of an experienced coder

You have to add the initial rotation to the new rotation. So make a variable for the initial c1 or c0 and simply add the rotations. In this case your tank rotation is what you should add onto your new rotation. It may be subtracting. But try adding it first

Can you go a little more in depth, i dont understand fully.
What i think your saying is create a number value and when i rotate the tank make the number value the number you rotate it and add it to the code?

Hey, i figured out a solution that i think is the best possible one.


By adding a number value and using this code

	weld.C0 = CFrame.new(weld.C0.Position) * CFrame.fromOrientation(0,ry - math.pi + script.Parent.Parent.TurnValue.Value,0)

And making it when the tank moves it edits the value to stay on the correct path (this is the easiest because using RUN its automatically on the server so you can modify the value to be correct)
like this:

game.TweenService:Create(script.Parent, TweenInfo.new(5), {CFrame = script.Parent.CFrame * CFrame.Angles(0,90,0)}):Play()
game.TweenService:Create(script.Parent.Parent.TurnValue, TweenInfo.new(5), {Value = -2.05}):Play()

he’s talking about the initial rotation, that is, where the part is initially pointing to