Raycast not giving right position

  1. What do you want to achieve? Keep it simple and clear!
    I want the bullet to go where i click my mouse
  2. What is the issue? Include screenshots / videos if possible!
    for some reason it goes in the wrong direction
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i tried making the raycast starts from the player character but that made it worse
    here is a video showing the issue
    robloxapp-20240519-0845477.wmv (75.3 KB)

a localscript when a player activates the gun

local ForceVector

script.Parent.Activated:Connect(function()
	local UIS = game:GetService("UserInputService")
	local MouseLocation = UIS:GetMouseLocation()
	local Cam = game.Workspace.CurrentCamera
	local MouseVector = Cam:ViewportPointToRay(MouseLocation.X,MouseLocation.Y)
	local Params = RaycastParams.new()
	Params.FilterDescendantsInstances = {game.Players.LocalPlayer.Character}
	Params.FilterType = Enum.RaycastFilterType.Exclude
	local RootPartCF
	local Result = workspace:Raycast(MouseVector.Origin,MouseVector.Direction * 10000,Params)
	if Result.Position.X > game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame.X then
		local RootPart = game.Players.LocalPlayer.Character.HumanoidRootPart	
		RootPartCF = CFrame.lookAt(RootPart.Position,Vector3.new(RootPart.Position.X + 50,RootPart.Position.Y,RootPart.Position.Z))
	else
		local RootPart = game.Players.LocalPlayer.Character.HumanoidRootPart
		RootPartCF = CFrame.lookAt(RootPart.Position,Vector3.new(RootPart.Position.X + -50,RootPart.Position.Y,RootPart.Position.Z))
	end
	ForceVector = Vector3.new(Result.Position.X,Result.Position.Y,2)
	script.Parent.RemoteEvent:FireServer(RootPartCF)
end)


script.Parent.RemoteEvent.OnClientEvent:Connect(function()
	local Bullet = Instance.new("Part",workspace)
	local Attachment = Instance.new("Attachment",Bullet)
	local VectorForce = Instance.new("VectorForce",Bullet)
	Bullet.Size = Vector3.new(0.25, 1, 0.25)
	local BulletStart = script.Parent.BulletStart.Position
	Bullet.Position = Vector3.new(BulletStart.X,BulletStart.Y,2)
	Bullet.Color = Color3.fromRGB(255, 255, 0)
	Bullet.Material =Enum.Material.Neon
	VectorForce.Attachment0 = Attachment
	VectorForce.Enabled = true
	VectorForce.ApplyAtCenterOfMass = true
	VectorForce.RelativeTo = Enum.ActuatorRelativeTo.World
	VectorForce.Force = ForceVector
	local Db = {}
	Bullet.Touched:Connect(function(hit)
		if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") and not Db[hit.Parent] then
			Db[hit.Parent] = true
			hit.Parent.Humanoid:TakeDamage(10)
			Bullet:Destroy()
			task.wait(0.2)
			Db[hit.Parent] = false
		else
			Bullet:Destroy()
		end
	end)

end)

The script when the remote event is fired

script.Parent.RemoteEvent.OnServerEvent:Connect(function(plr,ForceVector,RootPartCF)
	script.Parent.RemoteEvent:FireAllClients(ForceVector)
	plr.Character.HumanoidRootPart.CFrame = RootPartCF
end)

You were shooting into the sky. The sky does not have a position, so that’s maybe why and if it still proceeds to shoot the wrong direction then please reply to this message and I’ll figure something out.

no there is an invisible wall behind the character

Maybe that’s why it is shooting the wrong direction? I mean your mouse is pointed towards the wall / the position of the mouse in 3D?

Can you explain me what do you excatly mean?

You shot the wall right? So the bullet went to that position. To the position of the mouse. (Hit position)

I am using a different way Read the script first before solving my problem

Well in that case… I’m sorry but I cannot help you. :sad:

1 Like

I am confused by the MouseVector.Origin, should it be the origin which is the in front of the barrel?