Add an offset to a CFrame

trying to make something look at a character, but it has an offset that isn’t working well. I’m trying to make it automatic cause this one script is running all towers.

Code:

local Cframe = CFrame.lookAt(Tower.Model.Head.Position,Target.HumanoidRootPart.Position * Vector3.new(1,0,1))
				Tower:FindFirstChildWhichIsA("Model").Head.CFrame = Cframe
				TweenService:Create(Tower:FindFirstChildWhichIsA("Model").Head,TweenInfo.new(.2),{CFrame = Cframe * CFrame.Angles(Tower:FindFirstChildWhichIsA("Model").Head.Orientation.X,Tower:FindFirstChildWhichIsA("Model").Head.Orientation.Y,Tower:FindFirstChildWhichIsA("Model").Head.Orientation.Z)}):Play()

Are there any errors in the output window? This code is really confusing.

Do you want the offset to be in world space or in local space/object space

no errors its just rotated sideways. im trying to make it have an offset so it doesnt rotate sideways

Well looks like your adding an offset to the X and Z axis from the target position

But maybe showing a screenshot of what it looks like instead of just showing the code so we can help you properly :thinking:

I’m making it so it doesn’t point up. The tower defense is going to have air troops that only some can hit.

maybe define something like this?
local rotationOffset = CFrame.Angles(0, math.rad(urnumber), 0)

on “urnumber” position it to what angle you want?

Could you send a screenshot of what the offset looks like currently?

Image:
image
@envykillie the reason why I don’t have that is because this is the script for all towers meaning if I put offset for it all will be offsetted.

Also you should deconstruct the CFrame using ToEulerAngles instead of grabbing the Orientation

local TowerHead = Tower:FindFirstChildWhichIsA("Model").Head
local x,y,z = TowerHead.CFrame:ToEulerAnglesXYZ()

pardon can you explain that better? (I have smooth brain)

when you use the method ToEulerAngles what it does is it returns the approximate angles of that CFrame in that order, also you couldve just done Cframe * Head.CFrame.Rotation


???

It’s a method on a CFrame not on the object itself

Maybe you could define another variable for urnumber and change it when you want it to look at the character. I think it would be a longer code though

local rotationOffset = CFrame.Angles(0, math.rad(urnumber), 0)

@RocketSlither

I’m guessing the cannon head is a model right, maybe try playing around with its pivot position

image

image

that work because of this is the script for ALL towers.

Fixed:

Tower:FindFirstChildWhichIsA("Model").Head.CFrame = Cframe * Tower.Model.Head.PivotOffset.Rotation
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.