Need help fixing error with new tank turret

Ok, I’m working on fixing some of my old tanks, and I’m trying to make them like the game ‘Armored Patrol’ because my other try did not work out.

I’ve got the tank turret to rotate, it just wont point at my mouse when I press the ‘F’ key to rotate it.

a GIF demonstrating:
https://gyazo.com/f09eaf666c621403016692ea85ac54d6

Please help!

1 Like

Um. There is no code we can review?

Oh right, sorry i was half asleep when i posted this

The client script:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local UserInputService = game:GetService("UserInputService")

UserInputService.InputBegan:Connect(function(keyCode)
	if keyCode.keyCode == Enum.KeyCode.F then	
		local angle = Mouse.Hit.LookVector
		
		game.ReplicatedStorage.tankTurretEvent:FireServer(angle)
	end
end)

The server script:

local turret = script.Parent

local turretMain = turret.TurretMain
local gunMain = turret.GunMain

local turretWeld = turretMain.Weld
local gunWeld = gunMain.Weld

game.ReplicatedStorage.tankTurretEvent.OnServerEvent:Connect(function(player, angle)
	turretWeld.C0 = turretWeld.C0 * CFrame.Angles(0,0,angle.X)
end)

First of all you’re multiplying the turret C0 not its origin which makes it rotate relative to a new CFrame each click.

So what do I do to fix that? Do i add?

Make a variable which stores the original turretweld c0 then multiply that with the angle.

What do I write? I’m new to CFraming

Go and read roblox’s website on CFrame then it will give you a better understanding.

I dont understand what i am doing, please help

Hello, I don’t understand what I am doing, can you help me please?

Edit: I tried again, and it didnt work, it still doesnt point to the mouse

@RatiusRat says this

local Base = turretWeld.C0
game.ReplicatedStorage.tankTurretEvent.OnServerEvent:Connect(function(player, angle)
	turretWeld.C0 = Base * CFrame.Angles(0,0,angle.X)
end)

yes, but the issue is that it dont look at the mouse

Like in the gif in the main post

Lookvector doesn’t give you the angle, it only gives you a vector that’s used to multiply with another vector for an increment in the XYZ relative to that CFrame which is an alternative to multiplying 2 CFrames.

I see that the script is using lookvector, will it work better if you are in 1st person and looking from the point of the barrel? Also I advise creating a loop that will slowly add or subtract the tank rotation towards the mouse lookVector Until it is looking in the same direction (for realisim). BTW, I think that aiming the mouse into nothing might be problematic.

I just tried to do that, it just points the opposite way, (and if you are wondering, yes the front surface is correct)

Have you tried using Mouse.UnitRay Instead? Check here: Mouse | Roblox Creator Documentation
You might also want to read up on this: Ray | Roblox Creator Documentation

Ok, getting somewhere now, it points exactly at the mouse, but now the vent will only fire one time

Update: it can only turn 45 degrees

I just realized the problem with these is the camera position, if the camera is not looking from the point of view of the barrel, then it might be off, so it wont work in 3rd person very well.
What you could try is making a new ray pointing in the direction of the mouse.Hit.p or mouse postion in 3D space. The problem with this is rays can only point from one direction. The origin is the start of the ray (first property), then to get the direction of the ray (the second property) then make the client script fire to the server the mouse.Hit.p then use the mouse.Hit.p and subtract it by the ray origin (the front of the tank.) to find the ray direction. Note you will need a part at the front of the tank to define the ray origin.
something like this:

local origin = --part at the front of the tank.
game.ReplicatedStorage.tankTurretEvent.OnServerEvent:Connect(function(player, postion)
	local lookvector = position - origin
	turretWeld.C0 = turretWeld.C0 * CFrame.Angles(0,0,lookvector.X)
end)

Note: sorry for the late response, it took a bit of digging to get to this conclustion.

1 Like