How to fix shooting on mobile

Hello, I tried to make it possible to shoot with a button on the phone and I succeeded, but the bullet holes in front of viewmodel. I dont know how to explain, just look at screenshots.

Local script:

		while firing == true and equiped and ammo.Value ~= 0 do
			if not reloading then
				if player.Character.Humanoid.WalkSpeed < 16 then
					if player.Character.Humanoid.WalkSpeed <= 8 then
						recoilingnmod = 0.8
					else
						recoilingnmod = 0
					end
					mouse.TargetFilter = workspace.BulletStorage
					mouse.TargetFilter = arms
					if ismobile then --checks if player using phone
						script.Parent.StartFire:FireServer(workspace.CurrentCamera.CFrame.LookVector * 10000,arms.Model.Handle.GunFirePoint.WorldPosition,recoilingnmod)
					else
						script.Parent.StartFire:FireServer(mouse.Hit.p,arms.Model.Handle.GunFirePoint.WorldPosition,recoilingnmod)
					end
					player.PlayerScripts.VisualShot.VFireEvent:Fire(arms)
				end
			end
			task.wait(0.1)
		end

And server script(How bullet works)

function NewBullet(ViewmodelHandle)
	local bullet = Instance.new("Part")
	bullet.Position = ViewmodelHandle
	bullet.CanCollide = false
	bullet.Transparency = 1
	bullet.Size = Vector3.new(0.5,0.5,0.5)
	bullet.Name = "bullet"
	bullet.Parent = workspace.BulletStorage
	bullet:SetNetworkOwner(nil)
	return bullet
end
function applyPhysics(bullet,mousePos,recoilmod)
	local muzzlevel = 1000
	local direction = CFrame.new(bullet.Position,mousePos)

	local verticalspread = 1.5 - recoilmod
	local horizontalspread = 1.5 - recoilmod
	local rng = Random.new()

	verticalspread = rng:NextNumber(-verticalspread,verticalspread)
	horizontalspread = rng:NextNumber(-horizontalspread,horizontalspread)

	verticalspread = math.rad(verticalspread)
	horizontalspread = math.rad(horizontalspread)

	direction = direction*CFrame.Angles(verticalspread,horizontalspread,0)
	direction = direction.LookVector

	local mass =  bullet:GetMass()
	
	bullet:ApplyImpulse(direction * mass * muzzlevel)
end

Little bit of explanation: When i shoot bullet starts from Viewmodel’s firePoint(attachment), then it’s moves to mousepos, but on mobile i think it’s just moves straight.
Some ideas?

5 Likes

you should use something other than the mouse position if this is locked in FPS or set 3rd person Like casting a ray from the middle of the camera etc… few ways to do this even from end of barrel to middle of camera point

something like this might work

workspace:Raycast(Camera.CFrame.Position, Camera.CFrame.LookVector * Distance, raycastParams)
4 Likes

Where i should use this? And i’m already have raycast in my server script

3 Likes
function fire(player,mousePos,ViewmodelHandle,recoilmod)
	local bullet = NewBullet(ViewmodelHandle)
	local shell = NewShell(player)
	applyPhysics(bullet,mousePos,recoilmod)

	local runservice = game:GetService("RunService")
	local lastPos = bullet.Position
	local range = 10000

	while range > 0 do
		runservice.Heartbeat:Wait()
		local rayLength = (lastPos-bullet.Position).Magnitude
		range = range - rayLength

		local rayPos = bullet.Position
		local rayDirection = CFrame.new(lastPos,rayPos).LookVector

		local rayFilter = RaycastParams.new()
		rayFilter.FilterDescendantsInstances = {player.Character, workspace.BulletStorage, workspace.BulletHoles, workspace.ShellStorage, workspace.DropsStorage}
		rayFilter.FilterType = Enum.RaycastFilterType.Exclude

		local ray = workspace:Raycast(lastPos,rayDirection * rayLength,rayFilter)
1 Like

I think I just have to find the center of the screen, I find it anyway, but for some reason it doesn’t work

I figured out

				if ismobile then
					local rayFilter = RaycastParams.new()
					rayFilter.FilterDescendantsInstances = {player.Character, workspace.BulletStorage, workspace.BulletHoles, workspace.ShellStorage, workspace.DropsStorage,arms}
					rayFilter.FilterType = Enum.RaycastFilterType.Exclude
					local ray = workspace:Raycast(camera.CFrame.Position, camera.CFrame.LookVector * 10000)
					script.Parent.StartFire:FireServer(ray.Position,arms.Model.Handle.GunFirePoint.WorldPosition,recoilingnmod)
					else
						script.Parent.StartFire:FireServer(mouse.Hit.p,arms.Model.Handle.GunFirePoint.WorldPosition,recoilingnmod)
					end
1 Like

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