Good day everyone! I have a script inside a tool (gun) and I'm having problems RayCasting in a forward direction regardless of where the mouse is. I'm trying to follow a guide I seen on devforum but I'm having trouble. Now onto my code I have so far...
function RayCast(startPos, vec, rayLength) local hitObject, hitPos = game.workspace:FindPartOnRay(Ray.new(startPos + (vec * .01), vec * rayLength), Handle) if hitObject and hitPos then local distance = rayLength - (hitPos - startPos).magnitude if RayIgnoreCheck(hitObject, hitPos) and distance > 0 then -- there is a chance here for potential infinite recursion return RayCast(hitPos, vec, distance) end end return hitObject, hitPos end
function OnFireMobile() if IsShooting then return end if MyHumanoid and MyHumanoid.Health > 0 then if RecoilTrack and AmmoInClip > 0 then RecoilTrack:Play() end IsShooting = true isFiring.Value = 'true' print("Firing Gun") while ButtonClick and AmmoInClip > 0 and not Reloading do if Spread and not DecreasedAimLastShot then Spread = math.min(MaxSpread, Spread + AimInaccuracyStepAmount) UpdateCrosshair(Spread) end DecreasedAimLastShot = not DecreasedAimLastShot if Handle:FindFirstChild('FireSound') then Pitch.Pitch = .8 + (math.random() * .5) Handle.FireSound:Play() Handle.Flash.Enabled = true flare.MuzzleFlash.Enabled = true --Handle.Smoke.Enabled=true --This is optional end --if MyMouse then local targetPoint = MyMouse.Hit.p local shootDirection = (targetPoint - Handle.Position).unit -- Adjust the shoot direction randomly off by a little bit to account for recoil shootDirection = CFrame.Angles((0.5 - math.random()) * 2 * Spread, (0.5 - math.random()) * 2 * Spread, (0.5 - math.random()) * 2 * Spread) * shootDirection local hitObject, bulletPos = RayCast(Handle.Position, shootDirection, Range) local bullet -- Create a bullet here if hitObject then bullet = CreateBullet(bulletPos) end if hitObject and hitObject.Parent then local hitHumanoid = hitObject.Parent:FindFirstChild("Humanoid") if hitHumanoid then local hitPlayer = game.Players:GetPlayerFromCharacter(hitHumanoid.Parent) if MyPlayer.Neutral or (hitPlayer and hitPlayer.TeamColor ~= MyPlayer.TeamColor) then TagHumanoid(hitHumanoid, MyPlayer) hitHumanoid:TakeDamage(Damage) if bullet then bullet:Destroy() bullet = nil WeaponGui.Crosshair.Hit:Play() --bullet.Transparency = 1 end spawn(UpdateTargetHit) end end end AmmoInClip = AmmoInClip - 1 UpdateAmmo(AmmoInClip) --end wait(FireRate) end Handle.Flash.Enabled = false IsShooting = false isFiring.Value = 'false' flare.MuzzleFlash.Enabled = false --Handle.Smoke.Enabled=false --This is optional if AmmoInClip == 0 then Handle.Tick:Play() --WeaponGui.Reload.Visible = true Reload() end if RecoilTrack then RecoilTrack:Stop() end end end
the guide on devforum:
https://devforum.roblox.com/t/how-to-raycast-forward-without-using-players-mouse/348513/7
raycast_demo.rbxl
https://devforum.roblox.com/uploads/short-url/uKXCQMSWrDIdhmFCCgQp02aV6pj.rbxl
the gun I'm using
https://www.roblox.com/library/1533692920/Working-Pistol-Gun-WORKING?ViewInBrowser=true&CreatorId=411120421&SearchId=162E8FC5-2A2C-4A21-AC7B-14B63EF90523&Category=Model&Position=1&SearchKeyword=gun&CreatorType=User&SortType=Relevance