Making a mini-tank... hit a road bump

So I got Sofloan to build me a mini-tank for my upcoming game Disaster Island and you can see it here:


Isn’t it glorious?

So anyways, I’m scripting it to make it into a intelligent killing machine with advanced pathfinding, explosive shells, etc. But I’m currently having trouble with one small part of the tank’s looking mechanics, the top part of the tank or the head is meant to turn to the left and right and the nozzle is meant to look up and down at it’s target. At the moment I have 2 hinge constraints set up to do just that and they work perfectly (using servo type). The problem is scripting their target angles in order to look at the tank’s target. My first attempt worked only when the tank’s rotation is at 0,0,0 and whenever it’s at a different rotation the results are inaccurate to say the least.

local lookCF				= CFrame.new(verticalAim.Position, Torso.Position)
local X, Y, Z				= lookCF:toEulerAnglesXYZ()
verticalHinge.TargetAngle	= -math.deg(X)
horizontalHinge.TargetAngle	= -math.deg(Y)

So I was wondering if you guys could come up with a way to make the top part turn left and right in order to face the target player and have the nozzle turn up and down to face the player’s vertical position at all rotations. I’m stumped so I hope one of you have an idea of what to do, I’m assuming it’ll involve some math I have no knowledge of… or it might be an easy solution that I just didn’t think of. Either way, thanks for the help :slight_smile:

1 Like

Find lookCF in local space of the tank.

Could you give some example code?

local lookCF				= CFrame.new(verticalAim.Position, Torso.Position)
local lookCF = lookCF:toObjectSpace(tank.CFrame)
local X, Y, Z				= lookCF:toEulerAnglesXYZ()
verticalHinge.TargetAngle	= -math.deg(X)
horizontalHinge.TargetAngle	= -math.deg(Y)

Possibly

3 Likes

I actually just tried that. It works really well with the vertical hinge. It works with the horizontal hinge but it seems a bit behind. It doesn’t aim directly at the torso, it’s a little off.

EDIT: I fixed it, and it works completely. Case solved.

1 Like