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

	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))
		local RootPart = game.Players.LocalPlayer.Character.HumanoidRootPart
		RootPartCF = CFrame.lookAt(RootPart.Position,Vector3.new(RootPart.Position.X + -50,RootPart.Position.Y,RootPart.Position.Z))
	ForceVector = Vector3.new(Result.Position.X,Result.Position.Y,2)

	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 = {}
		if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") and not Db[hit.Parent] then
			Db[hit.Parent] = true
			Db[hit.Parent] = false


The script when the remote event is fired

	plr.Character.HumanoidRootPart.CFrame = RootPartCF

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?