Ive encountered a problem with my gun system basically the raycast isnt accurate when aimed close/beside a wall it shoots on the wall even tho im aiming on a clear zone, now my explanation is quite confusing so I prepared a demo video showing what my problem is
here’s the code being used
–
-- mouse pos here is just the mouse.hit.position
local Origin = Gun_ShootPoint.WorldPosition
local Direction = (MousePos - Origin).Unit * Range
local RayParams = RaycastParams.new()
RayParams.FilterDescendantsInstances = {Character}
RayParams.FilterType = Enum.RaycastFilterType.Blacklist
local InitialRay = workspace:Raycast(Origin,Direction,RayParams)
here’s where the origin is located if anybody’s wondering
why could this be happening? is it because of the position of the origin going to the direction intersecting thru the wall while travelling? I have no idea if anybody can help me Ill appreciate it
Are you sure that the mouse is directly in the screen? Also if your changing the mouse image id to the crosshair, you may wanna check to see if the middle point of the image is directly in the middle of the crosshair.
Head into first person. Aim, shoot and check to see if the bullet goes to your mouse position. If it doesn’t, its something wrong with the camera offset.
Probably cause the bullet starts from the gun and, even if it’s directed towards where you’re aiming, since the gun is in front of the wall the bullet collides with the wall first. Just a thought.
It’s because the raycast is coming from the character world model and not from the camera as @DEVLocalPlayer
Example below with casting ray from first person camera vs the world model animation:
This is more realistic but not good for gameplay purposes if you are aiming for responsive feel where the bullets go directly to the cross hair, the solution should be to raycast from the camera.Position instead.
I can think of making the aiming animation directed more towards the camera? Since I’ve noticed you made the CameraOffset more towards the right side of the screen, I guess you could try changing up the animation a little bit to match with the position of the camera? Basically just try to centre the gun as much as you can. I’ve experienced the same issue you have on other games so don’t worry it’s a normal problems developers have when making shooting games (unless it’s first person).
Or something else I can think of, you can change the velocity of the bullet based on your CameraOffset so it would be directed more towards where the camera is pointing rather than the mouse pointer.
so tried your solution changing the origin to the camera position and it worked!, but encountered a problem you can shoot thru the wall if you have the proper camera angle, any solutions I could try?
Hey! so just an update on what I did, basically just made it “realistic” the ray comes out of the barrel but I made a system where it would predict the bullet would go
anyways thx for your help Ill apply what youve said the next time I make a FPS gun which I would be making next