So, I have this tank that turns its turret with motor6D, but it only works correctly on 1 alightment. (I hope that make sense)
I’ve tried googling about this, but I only found very few relevant results… I did get to where I am now from the search, though. (It used to rotate randomly, but now there’s at least some consistency )
while _alive.Value and currentTarget ~= nil and (currentTarget.Position - self.Position).Magnitude < _range do
if currentTarget.Parent == nil then
break
end
local property = {
Position = currentTarget.Position
}
for i,turret in ipairs(turrets) do
local _attackBullet = GetBulletFromTable()
local _distance = (currentTarget.Position - self.Position).Magnitude
local _timeTaken = _distance/ _bulletSpeed
local p1 = turret.Position
local p2 = currentTarget.Position
local info = TweenInfo.new(_timeTaken, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)
local _faceDir = Vector3.new(p2.x, self.Position.y,p2.z)
local _forward = turret.CFrame:ToWorldSpace(CFrame.new(0,0,-1)).Position
local _faceAngle = CFrame.lookAt(turret.Motor6D.C1.Position, Vector3.new(_forward.x, p2.y, _forward.z))
local angle = GetAngle(p1, p2)
local turretC1 = CFrame.Angles(0,math.rad(angle),0)
_attackBullet.CFrame = CFrame.lookAt(p1, p2)
turret.Motor6D.C1 = CFrame.new() * self.CFrame:ToWorldSpace(turretC1) - self.Position -- Relative angle
-- uses tween v2 by steadyon
local tween = Tween:Create(_attackBullet, info, property)
tween:Play()
coroutine.resume(coroutine.create(function()
wait(_timeTaken)
ReturnBullet(_attackBullet)
local _attack
if currentTarget ~= nil then
if Logic.TakeDamage(currentTarget, _damage/ #turrets)== false then
-- takedamage returns false if target is dead
currentTarget = nil
end
end
end))
end
wait(_cooldown)
end
local function GetAngle (p1, p2)
local ratio = (p1.X-p2.X)/(p1.Z-p2.Z)
local beta = math.atan(ratio)
local beta = beta*(180/math.pi)
if (p1.Z-p2.Z)<0 then
if (p1.X-p2.X)<0 then -- Quad 3
beta= 180 + beta
else -- Quad 4
beta = -180 + beta
end
end
print(beta)
return beta
end
-- Got this from devforum, thanks person who wrote this!
This is what I have, do you know how I can fix my issue?
(Also bear with me if the way I calculate the Motor6D.C0 looks out of the ordinary, I literally just typed in whatever until it works. Been doing this for four hours, now. I think it’s time to ask people more knowledgable than me. lol)