Raycast Doesn't Work When I am Moving

  1. What do you want to achieve?
    I want my bullet to travel to my mouse location.

  2. What is the issue?
    The bullet to my gun doesn’t go to where my mouse is pointing, instead, it moves towards my face.

Here are some videos. (The “bullet” is marked in red so you can see it easily.)

A video of me while shooting the gun while standing still:
Shooting While Standing Still.wmv (83.5 KB)

A video of me shooting while moving:
Shooting While Moving.wmv (896.0 KB)


Here is my local script.

local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")

local g17 = script.Parent
local events = g17.Events
local g17DamageEvent = events.G17DamageEvent
local bulletEvent = events.BulletEvent

local G17BulletRenderer = require(g17.G17BulletRenderer)

local MAX_MOUSE_DISTANCE = 1000
local MAX_G17_DISTANCE = 4898.571

local function getWorldMousePosition()
	local mouseLocation = UserInputService:GetMouseLocation()
	local screenToWorldRay = workspace.CurrentCamera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y) -- Creates a ray from the 2D mouse location.
	local directionVector = screenToWorldRay.Direction * MAX_MOUSE_DISTANCE -- The unit direction vector of the ray multiplied by a maximum distance.
	local raycastResult = workspace:Raycast(screenToWorldRay.Origin, directionVector) -- Raycast from the ray's origin towards its direction.
	
	if raycastResult then
		return raycastResult.Position -- Return the 3D point of intersection.
	else
		return screenToWorldRay.Origin + directionVector -- No object was hit so calculate the position at the end of the ray.
	end
end

local function fireWeapon()
	local mouseLocation = getWorldMousePosition()
	local targetDirection = (mouseLocation - g17.Barrel.Position).Unit -- Calculate a normalised direction vector and multiply by laser distance.
	local directionVector = (mouseLocation - g17.Barrel.Position).Unit * MAX_G17_DISTANCE -- The direction to fire the weapon, multiplied by a maximum distance.
	
	-- Ignore the player's character to prevent them from damaging themselves.
	local weaponRaycastParams = RaycastParams.new()
	weaponRaycastParams.FilterDescendantsInstances = {Players.LocalPlayer.Character}
	local weaponRaycastResult = workspace:Raycast(g17.Barrel.Position, directionVector, weaponRaycastParams)
	
	-- Checks if any objects were hit between the start and end position.
	local hitPosition
	
	if weaponRaycastResult then -- If weaponRaycastResult has a value, then check for an enemy. 
		hitPosition = weaponRaycastResult.Position
		local enemyModel = weaponRaycastResult.Instance:FindFirstAncestorOfClass("Model")
		
		if enemyModel then -- If an enemyHumanoid is found in the model, then it is an enemy.
			local enemyHumanoid = enemyModel:FindFirstChild("EnemyHumanoid")
			if enemyHumanoid then
				g17DamageEvent:FireServer(enemyModel)
			end
		end
	else -- Else, calculate the end position based on the maximum bullet distance.
		hitPosition = g17.Barrel.Position + directionVector
	end
	
	bulletEvent:FireServer(hitPosition)
	G17BulletRenderer.createLaser(g17.Barrel, hitPosition)
end

-- Runs when the g17 is activated.
local function g17Activated()
	fireWeapon()
end

g17.Activated:Connect(g17Activated)

And here is the module script that renders the bullets.

local G17BulletRenderer = {}

local g17 = script.Parent
local Debris = game:GetService("Debris")

local SHOT_DURATION = 0.25 -- Amount of time that the bullet is visible for.

-- Create a bullet from a start position towards an end position.
function G17BulletRenderer.createLaser(g17Handle, endPosition)
	local startPosition = g17Handle.Position
	
	local bulletDistance = (startPosition - endPosition).Magnitude
	local bulletCFrame = CFrame.lookAt(startPosition, endPosition) * CFrame.new(0, 0, -bulletDistance / 2)
	
	-- Renders the bullets.
	local bullet = Instance.new("Part")
	bullet.Size = Vector3.new(0.2, 0.2, bulletDistance)
	bullet.CFrame = bulletCFrame
	bullet.Anchored = true
	bullet.CanCollide = false
	bullet.Color = Color3.fromRGB(225, 0, 0)
	bullet.Material = Enum.Material.Neon
	bullet.Parent = workspace
	
	-- Add bullet to the Debris service to be removed.
	Debris:AddItem(bullet, SHOT_DURATION)
end

return G17BulletRenderer

In your getWorldMousePosition(), would filtering out the player character in the raycast change anything? You seem to have done that in the fireWeapon() function, but not in getWorldMousePosition() which seems to AFFECT what fireWeapon() raycasts.

Okay, I will try this.



Your solution worked. All I had to do was put "weaponRaycastParams " inside of the getWorldMousePosition() function.

1 Like