Aiming AI tank system

I’m have no idea how to do the AI tank aiming system.


There are two hinges from the tank.
One of them rotates, and the other one makes the weapon tilt.
I’m trying to use CFrame.new(p1,p2) but All of them are failed.

3 Likes

By ‘hinges’ - do you mean the native “Hinge constraint” provided by ROBLOX or do you mean a point where you intended for the turret to pivot? Additionally, could you provide a bit more detail about how using CFrame.new(p1,p2) failed?

-- Inputs
local base = --(CFrame of the tank's chassis)
local target = --(Vector3 position of the target)

-- Position of target relative to the tank
local localizedTarget = base:PointToObjectSpace(target)

-- Values the hinges need to be set at to point the weapon at the target
local yaw = math.atan2(-localizedTarget.X, -localizedTarget.Z)
local pitch = math.atan2(localizedTarget.Y, math.sqrt((localizedTarget.X ^ 2) + (localizedTarget.Z ^ 2)))

print("yaw: "..math.deg(yaw))
print("pitch: "..math.deg(pitch))
2 Likes

In this code I assumed that you were using HingeConstraints to move the turret.

all you’ll have is an automated tank

machine learning is not possible. studio scripting is way too limited. the text editor doesn’t even have basic features.

whenever im programmingin lua, i do it in Visual studio code and then move it over to studio. its super annoying to test, but i feel like i dont have any other options

He’s not building a Terminator. He just wanted to know the calculations to make a tank point a gun at a target.

2 Likes

Lol, it stated to dance. because of ServoMaxTorque?

1 Like

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.