Aiming AI tank system

Please share. I would be delighted to see a tank system dance


Is anything wrong?

3 Likes

Pointing into the wrong direction, I tried to put multiple tanks.
There are pointing to the same direction which is not target’s position.

Almost a year ago, I was trying to create a tower defense. I gave up when I couldn’t figure out this exact problem.

If anyone figures it out, please mention me. My TD may have hope yet. Good luck OP!

1 Like

Oh it was out of range. but it’s still point to the wrong direction.

What I have done in the past is to use a bodygyro and then edit it’s cframe with a basic CFrame.new(positionOfTank, whatTheTankIsPointingAt)

Works most of the time for me

How can I divide the CFrame into Rotation and Tilt? That’s my question.

Try this sample code from a DevHub article on CFrames, and adapt it to your tank?

Code
function lookAt(target, eye)
    local forwardVector = (eye - target).Unit
    local upVector = Vector3.new(0, 1, 0)
    -- You have to remember the right hand rule or google search to get this right
    local rightVector = forwardVector:Cross(upVector)
    local upVector2 = rightVector:Cross(forwardVector)
 
    return CFrame.fromMatrix(eye, rightVector, upVector2)
end
2 Likes

@d1tr: Actually, there was a post proven that AI is possible in Lua. There was a post by @Kironte who developed a Neural Network Library module. Another demo developed by @ScriptOn developed an AI cop car as a demo for Vehicle Simulator, proving this in the following video.

@DetailedModuler: Although you want to learn AI, you might need to learn a higher level of programming, but looking through this post is always worth a shot at learning.

5 Likes

What value are you using for the “base” CFrame in the code I provided? Are you using the CFrame of the gun itself rather than something from the vehicle it is mounted on? I wrote the code with the assumption that “base” will represent a CFrame that is facing the frontwards direction of the tank. Also did you make sure that the rotation and tilt values are being converted to degrees?

I’m using the barrel of the gun for the base CFrame value.
And I just copy most of them, but it just won’t work.
Or there is something wrong with other script in the tank?

Would like to just note that @ScriptOn’s AI cop car demo was made long before my module. My module just introduced a fleshed out library for everyone to use. Thanks for the mention though!

2 Likes

Base needs to be the part that the gun is mounted on. Since the script calculates the angle the gun needs to rotate to face the target, if you use the gun itself as the base it will be constantly changing and creating a sort of “dog chasing it’s tail” effect, which is what it looks like in your video.

Ah, my mistake! I misread your forum post, I made a correction to my response, and as well included the AI Vehicle demo by ScriptOn. My apologies!

It’s not moving now. Cries :frowning:
Wrong Base?

local Service = game:GetService(“RunService”)
local Rotate = script.Parent.Weapon.Rotater.Hinge
local Tilt = script.Parent.Weapon.Bar.Hinge
Service.Stepped:Connect(function()
local Target = script.Parent.Aiming.Value
if Target ~= nil then
local localizedTarget = script.Parent.Base.CFrame:PointToObjectSpace(Target.Head.Position)
local yaw = math.atan2(-localizedTarget.X, -localizedTarget.Z)
local pitch = math.atan2(localizedTarget.Y, math.sqrt((localizedTarget.X ^ 2) + (localizedTarget.Z ^ 2)))
Rotate.TargetAngle = math.deg(yaw)
Rotate.TargetAngle = math.deg(pitch)
end
end)

You’re setting the same hinge for both angles.

Making an AI for this doesn’t need to be super complicated, it’s just a simple algorithim, that checks if the player is in range, and if so check if there is something in the way of the ray that it’s firing, and if not shoot.

I am not very good with AIs but I can suggest;
There is a tutorial on youtube called
“Tank AI” by Y3llow Mustang. He is a very good scripter.

Video link.

Ahhhh, It’s not aiming right.
RobloxScreenShot20200125_095533172

Oh I use another way to aim the gun right.
I used bodygyro to aim it it.
It worked pretty well XD.

1 Like