Gun raycast doesn't position part at the correct hit point

What is the issue? Include screenshots / videos if possible!
– My Script doesn’t raycast properly. When I place a part at the hit point, it places the part at some random position.

What solutions have you tried so far? Did you look for solutions on the Developer Hub?
– I’ve tried ignoring the viewmodel.

					-- raycast and shoot ( main )
				
					if currentservermodel then
						
						local shootpoint = righteff.CFrame * CFrame.new(0,0,-0.5)
						
						local rayshoot = Ray.new( shootpoint.p , cam.CFrame.LookVector * 5000 )
						local ignorelist = { char , lasttool.ViewModel }
						
							for i , v in pairs( workspace:GetChildren() ) do
								
								if v:IsA("Part") or v:IsA("MeshPart") or v:IsA("UnionOperation") then
								
									if v.CanCollide == false then
										
										table.insert( ignorelist , v )
										
									end
								
								end
								
							end
						
						local hitobj , hitpos , normal = workspace:FindPartOnRayWithIgnoreList( rayshoot , ignorelist )
						
						
						local part = Instance.new("Part")
						part.Anchored = true
						part.CanCollide = false
						part.Position = hitpos
						part.Size = Vector3.new(0.2,0.2,0.2)
						part.Parent = workspace					
						part.Material = Enum.Material.Neon
						
						cam.CameraType = Enum.CameraType.Scriptable							
						cam:Interpolate( CFrame.new( hitpos.X , hitpos.Y , hitpos.Z ) * CFrame.new(5,5,5) , CFrame.new( hitpos.X , hitpos.Y , hitpos.Z ) , 1 )
						wait(1)
						cam.CameraType = Enum.CameraType.Custom
										
					end

The Camera Should move to the hit spot but it doesn’t.
It works fine at the start but doesn’t work at all after a few tries.

Here’s the link to the game-

-Link to game-

What is the problem?

1 Like

Try exchanging this part here,

image

with,

part.CFrame = CFrame.new(hitpos)

Not saying this will help your case, but just a possibility, because this occured to me before and it was just because position takes collisions into consideration. Worth a try :+1:

Ok. :slightly_smiling_face: Will try.

No , doesn’t work. :confused: (30 characters)

Need a lot more information such as:

  • Is this a local script or a server script?
  • Where is the script located?
  • More code
  • What exactly the problem is. You said the camera doesn’t move at all, but then you also said it works fine at the start.
  • More descriptive as to what exactly you want the script to do. I tested it out in studio and I’m guessing you want the camera to zoom in on the bullet but I’m not sure.

~BuDeep

It’s a local script in StarterGui.

Yes.
I don’t know what the problem is. There’s a part rigged to the ViewModel from which the script raycasts towards the camera’s lookvector , and then creates a part where the ray was hit. Not sure why it doesn’t work.
I can make the game open for you to check it.

Here it is- Game’s Link (Can be edited)

Weird, sounds like the issue is either in your camera or your ignoretables, try replacing this,
image

local shootpoint = righteff.CFrame * CFrame.new(0,0,-0.5)
						
						local rayshoot = Ray.new( shootpoint.p , cam.CFrame.LookVector * 5000 )
						local ignorelist = { char , 

with these,

local shootpoint = righteff.CFrame * CFrame.new(0,0,-0.5)
						
						local rayshoot = Ray.new( shootpoint.p , (game.Players.LocalPlayer:GetMouse().hit.p - shootpoint.p).unit * 5000 )
						local ignorelist = { char , 

If this solves your issue then the problem is most likely with your camera :+1:

1 Like

Still doesn’t work. ( 30 30 30 30 )

You mean not a single change?
I’m sure that will change at least something (?)

Let me see the changed version you got of the code u posted in OP

You can check it out I published it again after the changes.

Look vector points in the negative Z direction, so it might be the reason behind your issues. Just negate the 5000 part. Also, rays can’t go beyond 1000.

Negating it will just make the ray go backwards right? I tried it and it doesn’t work.

I can’t access Roblox right now, so I can’t check for myself.

Does anything happen to the camera once it returns though? Try replacing the cam.CFrame.LookVector to the look vector of whatever the tool’s barrel is. If that stops breaking, then it’s an issue with the camera.

Currently I’ve just abandoned this project, so I don’t need any more replies now.

2 Likes