What do you want to achieve?
Hello, I’m working on a prision game and we trying to make the things a little bit different. For example at the gas station robbery we want to make something like what GTA does, that when you aim your gun to the NPC he raise his hands and he open something and that gives you money.
There’s a video
What solutions have you tried so far?
Well I’ve tried to watch youtube tutorials about it and I can’t find nothing. I was thinking about detecting when the player has the gun equiped, but what will not work because the animation gonna work even before you aim to the target.
You can use a raycast to see when the gun is pointed at the npc, however i think it might be cool to still have the npc react when they pull the gun out.
I asked chat GPT and he give me the same answer. I think I am gonna try that, I’m sadly not that advanced, but I’ll will try to make it work. Also thanks for the answer !
Usually I don’t provide all the code to such systems due to my no-spoonfeeding policy but you are in luck since I have nothing better to do rn.
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local tool = script.Parent or wherever you have your tool.
local npc = workspace.NPC
local isEquipped = false
tool.Equipped:Connect(function()
isEquipped = true
end)
tool.Unequipped:Connect(function()
isEquipped = false
end)
-- Thanks to @nicemike40 for this function
local function IsPlayerInLineOfSight(playerHead, npcHead, maxAngle)
local lookDirection = playerHead.CFrame.LookVector
local towardsNpc = (npcHead.Position - playerHead.Position).Unit
local dotProduct = lookDirection:Dot(towardsNpc)
local cosineAngle = math.cos(maxAngle)
return dotProduct >= cosineAngle
end
RunService.Stepped:Connect(function()
if isEquipped and IsPlayerInLineOfSight(character.Head, npc.Head, 60) then
--Play loaded animation
end
end)