Top down gun game problem

The barrel of the gun is not pointing where the mouse point is pointing.
A scenario is needed to solve this problem.

Ekran Görüntüsü (9)

2 Likes

Please copy and paste your script here.
Put 3 backticks (```) before and after the script so it displays properly.

2 Likes
local char = plr.Character
local hrp = char.HumanoidRootPart
local mouse = plr:GetMouse()
local RunService = game:GetService("RunService")

RunService.RenderStepped:Connect(function()
	local hrpPos, mousePos = hrp.Position, mouse.Hit.Position
	hrp.CFrame = CFrame.new(hrpPos, Vector3.new(mousePos.X, hrpPos.Y, mousePos.Z))
end)
2 Likes

It’s because you are rotating the whole character to look towards the mouse point.
You are making the player’s head and body look towards the mouse, not the gun.

1 Like

you could use the CFrames between the gun and the humanoid root part to get a constant offset that you add to the cframe look at so the gun points to the mouse.

1 Like

not sure how, but you should rotate the RootPart to make the weapon aim the mouse, not the RootPart

1 Like

I’m sorry, but I’m new to script writing, can you write a script for me?

1 Like

ok, i think i get it, but is a bit strange:

wait(2)
local RunService = game:GetService("RunService")

local plr = game:GetService("Players").LocalPlayer
local char = plr.Character
local hrp = char.HumanoidRootPart
local mouse = plr:GetMouse()
local handle = script.Parent:WaitForChild("Handle")

local hanOffset = hrp.CFrame:Inverse() * handle.CFrame
local hanAngleOffset = hanOffset - hanOffset.Position
local rX,rY,rZ = hanAngleOffset:ToOrientation()
RunService.RenderStepped:Connect(function()
	local tar = mouse.Hit.Position
	local tar2 = Vector3.new()
	
	if hanOffset.Position.Magnitude > (tar - hrp.Position).Magnitude then
		warn("to close")
		return
	end

	tar2 = ( CFrame.new(handle.Position,tar) - handle.Position + tar):ToWorldSpace(hanOffset:Inverse()).Position

	--hrp.CFrame = CFrame.lookAt(hrp.Position,tar2) * hanAngleOffset:Inverse()
	hrp.CFrame = CFrame.lookAt(hrp.Position,Vector3.new(tar2.X,hrp.Position.Y,tar2.Z)) * CFrame.fromOrientation(0,rY,0):Inverse()
end)

Try it, if anyone wants to optimize it, please do it, and sometimes you character will start flying i dont know why, and you cant aim your gun if you are too close to the target, maybe will be better to rotate the tool instead of the character?

i will let this test model that i make for making this https://create.roblox.com/store/asset/16198642547

Thank you, but this is the situation right now. My gun has no handle. I replaced the handle with “BodyAttach”

1 Like

no, use the gun body, like the actually black metal part

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.