Stuck on some cframe manipulation

Working on some turrets but been having issue w the rotation, when the turret itself tilts it doesnt aim properly
excuse the dictionary usage.

local function Aim()
	local AimTemp = {}
	AimTemp["CurrentCF"] = BS.CFrame
	AimTemp["OffSet"] = (Vector3.new(0,TS.CFrame.Position.Y,0) - Vector3.new(0,BS.CFrame.Position.Y,0))

	AimTemp["TurretFoward"] = AimTemp["CurrentCF"]:VectorToObjectSpace(Vector3.new(0,0,-1)) -- LookVector
	AimTemp["TurretUp"] = AimTemp["CurrentCF"]:VectorToObjectSpace(Vector3.new(0,1,0)) -- UpVector

	AimTemp["LookAtDirection"] = (ST.Position - AimTemp["CurrentCF"].Position).Unit --NewLookVector

	AimTemp["DotProduct"] = AimTemp["TurretUp"]:Dot(AimTemp["LookAtDirection"]) -- Decides if its +/- on Up/Down from a value from -1 to 1

	AimTemp["Up/DownAngle"] = math.deg(math.asin(AimTemp["DotProduct"])) -- Uses that value from -1 to 1, to make a angle

	AimTemp["DotProduct"] = AimTemp["TurretFoward"]:Dot(AimTemp["LookAtDirection"]) -- Decides if its +/- on Up Left/Right from a value from -1 to 1

	AimTemp["CrossProduct"] = AimTemp["TurretFoward"]:Cross(AimTemp["LookAtDirection"]) -- Gets The direction the turret will be facing

	AimTemp["Left/RightAngle"] = math.deg(math.atan2(AimTemp["CrossProduct"].Y,AimTemp["DotProduct"]))
	
	BS.Orientation = BS.Orientation + Vector3.new(0,AimTemp["Left/RightAngle"],0)
	print(AimTemp["Left/RightAngle"])
	TS.Orientation = BS.Orientation + Vector3.new(AimTemp["Up/DownAngle"],0,0)
end

ive already tried using object space and pointto but no luck
any help would be appreciated

1 Like

You can use CFrame:LookAt and then turn it into orientation, you will get 3 axis angles that you have rotate your sentry to achieve perfect rotation

yea this in a senario where you cannot directly set a parts cf

its controlled thro mechanisums like servos/motors. its a nice challenge tbf (till I get stuck like rn lol)

if you need more context its for use within another game (waste of space) that ive been using for scripting practice

also i have tried this which dont work out so great for whatever reason

local function Aim2()
	local AimTemp = {}

	-- Get the current CFrame of the bottom servo
	AimTemp["CurrentCF"] = BS.CFrame

	-- Calculate the target CFrame using lookAt
	local TargetCF = CFrame.lookAt(AimTemp["CurrentCF"].Position, ST.Position)
	local Diff = AimTemp["CurrentCF"]:toObjectSpace(TargetCF)

	local X, Y, Z = Diff:ToEulerAnglesXYZ()

	-- Adjust the servo orientations
	BS.Orientation = Vector3.new(math.deg(X), math.deg(Y), 0)
	TS.Orientation = Vector3.new(math.deg(X), math.deg(Y), 0)
end