Raycast facing backwards of player instead of in front

So, I’m making a character-like fighting game, and when I tried raycasting for the first time – this is what I got.

Basically when I shoot, the minigun shoots a bullet (still in progress). However, it shoots behind the player instead of what I’m trying to achieve, which is in front of the player. Also, I have no idea how to increase the range of the raycast, I’ve heard its from the direction, but when I tried changing it (origin - direction), (origin + direction), it still ended up the same way. The one in the photo below is origin - direction.

Also, its noted that when the player shoots, a block in front of the player spawns about 30 studs in front of the player that way the bullet can show without wasting ammo for the player. (which is why theres so many grey boxes behind me)

Code:

	repeat wait(0.1)
			if ranout == false then
				local params = RaycastParams.new()
				params.FilterType = Enum.RaycastFilterType.Exclude
				params.FilterDescendantsInstances = {game.Workspace:FindFirstChild(player.Name), game.Workspace:FindFirstChild(player.Name).meshes:GetChildren()}
				
				local max = game.Workspace:WaitForChild("MINIGUNNERmax"):Clone()
				max.Name = player.Name .. " max"
				max.Parent = workspace
				
				local dist = -30
				max:SetPrimaryPartCFrame(CFrame.new(Character.meshes.Meshes.CFrame.Position+(dist*Character.meshes.Meshes.CFrame.LookVector),Character.meshes.Meshes.Position)*CFrame.Angles(0, math.rad(0), 0))
				
				local origin = game.Workspace:WaitForChild(player.Name).meshes.shootarea.Position
				
				local params = RaycastParams.new()
				params.FilterType = Enum.RaycastFilterType.Exclude
				
				params.FilterDescendantsInstances = {
					game.Workspace:FindFirstChild(player.Name), 
					game.Workspace:FindFirstChild(player.Name).meshes:GetChildren(),
					game.Workspace.INGREDIENTSPART:GetChildren()
				}
				
				
				
				
				
				
				params.IgnoreWater = true
				
				local direction = max.Part.Position
				
				local ray = workspace:Raycast(origin, origin-direction, params)
				
				local shootarea = game.Workspace:FindFirstChild(player.Name).meshes.shootarea
				
				if ray then
					print("hi!")
					print(ray.Instance.Name)
					
					local Dis = (ray.Position - shootarea.Position).magnitude
					
					local Dir = (ray.Position - shootarea.Position)
					
					local MiddlePoint = shootarea.Position + Dir/2
					
					local bullet = Instance.new("Part",workspace)
					bullet.Name = "minigunBullet" .. " player.Name"
					bullet.CFrame = CFrame.new(MiddlePoint,ray.Position)
			
					bullet.Size = Vector3.new(0.5,0.5,Dis)
					bullet.Material = Enum.Material.Neon
					bullet.CanCollide = false
					bullet.Anchored = true
					
					coroutine.resume(coroutine.create(function()
						wait(2)
						local boolvalue = false
						
						for i = 0, 1, 0.05 do
							wait(0.03)
								bullet.Transparency = i
							
							if bullet.Transparency >= 1 then
								boolvalue = true
							end
						end
						
						bullet:Destroy()
					end))
				end
			end
		until playerMouse.Value == false

(also, sorry for the amount of spaces i put between each line, it helps me organize the code a bit more when i read it.)

ranout is a bool that detects if the player is out of bullets,

this is a portion of what is actually in the script (because the other parts are just for things like walkspeed change, status effects, etc)

there are no errors in the output.

this is a module script. (player is passed from module scripts. (i use module trees for the abilities.))

MINIGUNNERmax is the grey block (which is a model) that spawns in front of the player.

RaycastResult Distance shows the distance property.
Direction the sample script shows the format of how to set it.

I tried replacing this line of code:

local ray = workspace:Raycast(origin, origin-direction, params)

with the reference of this block of code

-- Cast the ray and create a visualization of it
	local raycastResult = Workspace:Raycast(originPosition, direction * distance)

	if raycastResult then
		-- Print all properties of the RaycastResult if it exists
		print(`Ray intersected with: {raycastResult.Instance:GetFullName()}`)
		print(`Intersection position: {raycastResult.Position}`)
		print(`Distance between ray origin and result: {raycastResult.Distance}`)
		print(`The normal vector of the intersected face: {raycastResult.Normal}`)
		print(`Material hit: {raycastResult.Material.Name}`)
	else
		print("Nothing was hit")
	end

to get this result:

local ray = workspace:Raycast(origin, direction * 100, params)

I tested it in game, and I got this:

It is going in front of the player now, but its tilted up.
Is there a way to make it so it only goes straight-forward instead of up?

I’m no pro but it’s probably due to ToWorldSpace vs ToObjectSpace.

I tried it out, and I applied :ToObjectSpace with the shootarea CFrame, and I adjusted it a bit to get this block of code.

	repeat wait(0.1)
			if ranout == false then
				local params = RaycastParams.new()
				params.FilterType = Enum.RaycastFilterType.Exclude
				params.FilterDescendantsInstances = {game.Workspace:FindFirstChild(player.Name), game.Workspace:FindFirstChild(player.Name).meshes:GetChildren()}
				
				
				
				local origin = game.Workspace:WaitForChild(player.Name).meshes.shootarea.Position
				
				local params = RaycastParams.new()
				params.FilterType = Enum.RaycastFilterType.Exclude
				
				params.FilterDescendantsInstances = {
					game.Workspace:FindFirstChild(player.Name), 
					game.Workspace:FindFirstChild(player.Name).meshes:GetChildren(),
					game.Workspace.INGREDIENTSPART:GetChildren()
				}
				
				
				local max = game.Workspace:WaitForChild("MINIGUNNERmax"):Clone()
				
				max.Name = player.Name .. " max"
				max.Parent = workspace

				local dist = 30
				max:SetPrimaryPartCFrame(CFrame.new(Character.meshes.Meshes.CFrame.Position+(dist*Character.meshes.Meshes.CFrame.LookVector),Character.meshes.Meshes.Position)*CFrame.Angles(0, math.rad(0), 0))
				
				params.IgnoreWater = true
				
				local direction = max.Part
				
				local length = 30
				
				local ray = workspace:Raycast(origin, (origin-direction.Position)*length, params)
				
				local shootarea = game.Workspace:FindFirstChild(player.Name).meshes.shootarea
				
				if ray then
					
					
					
					
					print("hi!")
					print(ray.Instance.Name)
					
					local Dis = (ray.Position - shootarea.Position).magnitude
					
					local Dir = (ray.Position - shootarea.Position)
					
					local MiddlePoint = shootarea.Position + Dir/2
					
					local bullet = Instance.new("Part",workspace)
					bullet.Name = "Laser"
					bullet.CFrame:ToObjectSpace(game.Workspace:FindFirstChild(player.Name).meshes.shootarea.CFrame)
					
						bullet.CFrame = CFrame.new(MiddlePoint,ray.Position)
					
					
			
					bullet.Size = Vector3.new(0.5,0.5,Dis)
					bullet.Material = Enum.Material.Neon
					bullet.CanCollide = false
					bullet.Anchored = true
					
					coroutine.resume(coroutine.create(function()
						wait(2)
						local boolvalue = false
						
						for i = 0, 1, 0.05 do
							wait(0.03)
								bullet.Transparency = i
								max.Part.Transparency = i
							
							if bullet.Transparency >= 1 then
								boolvalue = true
							end
						end
						
						bullet:Destroy()
						max:Destroy()
					end))
				end
			end
		until playerMouse.Value == false

So basically what I changed was that the :ToObjectSpace was added for the bullet so that the center of the world is to the shoot area near the gun.

But, after I changed the Units (-30) on the

local dist = -30

to

local dist = 30

I believe that since the direction was at the position of the grey block, since it was behind me, the raycast went in front? I have no idea how that happened, but it solved the problem, so I’m not complaining.

Anyways, thanks @Scottifly , I wouldn’t have solved this without your help.
also, sorry it took me an hour and a half to reply back, I was stuck with fixing the code to make it work in the meanwhile.

No worries, I wasn’t in any rush.

The -30 or +30 issue is from the Orientation of the Raycast Part. If you’d spun it around 180 degrees you would have got the same result.