- What do you want to achieve?
Hello! I want to make a gun with raycasting that shoots from a top down-view.
- What is the issue?
My issue is that everytime I fire the gun, the raycast is acting weird. It isn’t going towards the mouse, instead it appears to be hitting things not even close to the mouse sometimes.
First Video
Second Video
Output from the play session I did in the two videos.
Sorry if these videos don’t show much, but basically you can see that I am clicking on the dummy and the remote event is fully firing, sometimes it hits, sometimes it doesn’t, it’s very weird.
- What solutions have you tried so far?
The solutions I have tried so far are making a new variable named DIRECTION. The variable basically stored the Mouse position, but set the Y axis to the player’s humanoidrootpart Y axis. I replaced Mousehit in the script with the variable direction so that I could have the X and Z axis of the mouse, but keep the Y axis of the player. In theory I thought that could make a straight line to the mouse, but it didn’t change anything, the gun was still acting weird.
local Weapon = script.Parent
local Shoot = Weapon.Shoot
Shoot.OnServerEvent:Connect(function(Player,mousehit)
local Character = Player.Character
local Ammo = Weapon.Ammo
local AmmoReserves = Weapon.AmmoReserves
local MaxAmmo = Weapon.MaxAmmo
--- check ammo capacity and stuf
--- Fire -----------------------------------------------------------------------------------------------------------------------------------------------
if Ammo.Value > 0 then
Ammo.Value = Ammo.Value - 1
if Ammo.Value == 0 then
Weapon.ReloadText:FireClient(Player)
end
local Bullet = Instance.new("Part")
Bullet.BrickColor = BrickColor.new("New Yeller")
Bullet.Material = Enum.Material.Neon
Bullet.CanCollide = false
Bullet.Parent = game.Workspace
Bullet.Anchored = true
Bullet.CFrame = Character.HumanoidRootPart.CFrame
Bullet.Size = Vector3.new(0.1,0.1,10)
Bullet.CFrame = CFrame.new(mousehit,Character.HumanoidRootPart.Position)
local bd = Character.HumanoidRootPart.Position - Bullet.Position
Bullet.Size = Vector3.new(0.1,0.1,math.abs(bd.Z))
local gunparams = RaycastParams.new()
gunparams.FilterDescendantsInstances = {Character,Bullet}
gunparams.FilterType = Enum.RaycastFilterType.Blacklist
local RaycastResult = game.Workspace:Raycast(Weapon.Position,(mousehit),gunparams)
if RaycastResult then
local HitPart = RaycastResult.Instance
print(HitPart)
local Humanoid = HitPart.Parent:FindFirstChild("Humanoid")
if Humanoid ~= nil then
Humanoid:TakeDamage(30)
end
end
wait(0.1)
Bullet:Destroy()
end
--- Fire -----------------------------------------------------------------------------------------------------------------------------------------------
if Ammo.Value == 0 and AmmoReserves.Value > 0 then
Weapon.ReloadText:FireClient(Player)
end
if Ammo.Value == 0 and AmmoReserves.Value == 0 then
Weapon.OutOfAmmo:FireClient(Player)
end
end)
This is the code that fires the raycast. The raycast fires from a pistol welded onto the right arm of the player (Weapon variable.) The direction is mousehit which in the local script that fires the remote event named shoot is Mouse.Hit.p, or the player’s mouse pretty much.
What could I be doing wrong?