You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Fix my beam bullet tracer origin position.
What is the issue?
I tried to use Raycast for a gun and wanted to make a bullet tracer for it for whatever reason it doesn’t work the position where the bullet should come out is completely off
How it look in-game
What solutions have you tried so far? Tried switching from server to client still have the same issue.
The position I use is taken from the part cframe which I weld with the gun.
I made sure the cframe/position was in the right place. however, when I switch from client to server and then back to the client via the studio feature the position is correct and works just fine(it also fixes itself after some time has passed)
local ViewRayOrigin = workspace.CurrentCamera.CFrame.Position
local gunOrigin = game.Workspace:FindFirstChild("armsViewmodel")
if not gunOrigin then repeat print("No gun") wait() until gunOrigin end
local originPos=gunOrigin["Glock"]["BulletOrigin"].Position
local Range = 1500
local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {Players.Character,workspace.Effect}
local ViewRayDirection = (workspace.CurrentCamera.CFrame.LookVector + Vector3.new(math.random(-10,10)/500,math.random(-10,10)/500,math.random(-10,10)/500)) * Range
local ViewRay = workspace:Raycast(ViewRayOrigin, ViewRayDirection, Params)
if ViewRay and ViewRay.Position then
local distance = (originPos - ViewRay.Position).Magnitude
local p = game.ReplicatedStorage.BulletTracer:Clone()
p.Anchored = true
p.CanCollide = false
p.CanQuery=false --neat feature so you wont be shooting the bullets incase raycast fails
p.CanTouch=false
p.Size = Vector3.new(0.1, 0.1, distance)
p.CFrame = CFrame.new(originPos,ViewRay.Position)*CFrame.new(0, 0, -distance/2)
p.Attachment1.Position = Vector3.new(0,0,-distance/2)
p.Attachment2.Position = Vector3.new(0,0,(distance/2))
p.Parent = workspace.Effect
game.Debris:AddItem(p,4)
game.TweenService:Create(p.BulletTrace,TweenInfo.new(0.8,Enum.EasingStyle.Quart),{Width0 = 0}):Play()
game.TweenService:Create(p.BulletTrace,TweenInfo.new(0.8,Enum.EasingStyle.Quart),{Width1 = 0}):Play()
end
I would recommend setting Attachment1.WorldPosition to BulletOrigin.Position. The Position property is treated as an offset position relative to the Attachment’s parent position. I hope this helps!
local armsViewModel = workspace:WaitForChild("armsViewmodel")
local glock = armsViewModel:FindFirstChild("Glock")
local bulletOrigin = glock:FindFirstChild("BulletOrigin")
p.Attachment1.WorldPosition = bulletOrigin.Position
still got the same result.
this video is me using the solution you gave me so maybe it could help. thanks for replying.
also, the first code also has the same result as in this video(kinda)
I’m not sure what the entire layout of your script is, but i think you should reiterate the ViewRayOrigin before setting the raycast, since storing values such as Vector3 will remain static until updated.
VERY MAYBE this might be the fix, but I just changed some things up a little bit.
local ViewRayOrigin = workspace.CurrentCamera.CFrame.Position
local gunOrigin = game.Workspace:FindFirstChild("armsViewmodel")
if not gunOrigin then repeat print("No gun") wait() until gunOrigin end
local originPos=gunOrigin["Glock"]["BulletOrigin"].Position
local Range = 1500
local Params = RaycastParams.new()
Params.FilterDescendantsInstances = {Players.Character,workspace.Effect}
local ViewRayDirection = (workspace.CurrentCamera.CFrame.LookVector + Vector3.new(math.random(-10,10)/500,math.random(-10,10)/500,math.random(-10,10)/500)) * Range
local ViewRay = workspace:Raycast(ViewRayOrigin, ViewRayDirection, Params)
if ViewRay and ViewRay.Position then
local distance = (originPos - ViewRay.Position).Magnitude
local p = game.ReplicatedStorage.BulletTracer:Clone()
p.Anchored = true
p.CanCollide = false
p.CanQuery=false --neat feature so you wont be shooting the bullets incase raycast fails
p.CanTouch=false
p.Size = Vector3.new(0.1, 0.1, distance)
p.CFrame = CFrame.new(originPos,ViewRay.Position)*CFrame.new(0, 0, -distance/2)
p.Attachment1.Position = Vector3.new(0,0,-distance/2)
p.Attachment2.Position = Vector3.new(0,0,(distance/2))
p.Parent = workspace.Effect
game.Debris:AddItem(p,4)
game.TweenService:Create(p.BulletTrace,TweenInfo.new(0.8,Enum.EasingStyle.Quart),{Width0 = 0}):Play()
game.TweenService:Create(p.BulletTrace,TweenInfo.new(0.8,Enum.EasingStyle.Quart),{Width1 = 0}):Play()
end
How do you insert the armsViewmodel, is it pre-inserted in server?
Because if that is the case, it might be a network ownership issue.
Best solution at that point is to maybe have the armsviewmodel parented to something like your script, and then be parented to camera, whilst also keeping a global value to make sure you wont have to rewrite the same thing over and over.
local camera=game.Workspace.CurrentCamera
local armsViewModel=script:FindFirstChild("armsViewmodel") or camera:FindFirstChild("armsViewmodel")
armsViewModel.Parent=camera --usual case with these FPS views is to parent it to the camera
Both the armsViewmodel and the bullet tracer script are on the client side (I intended to make a separate server-side bullet tracer later) and the armsviewmodel is just chilling(parented) in the workspace and I make the Cframe of the humanoidrootpart of the ViewModel set to camera Cframe every frame.
Even though it’s quite a hassle to use this method but it does work fine, Thank you very much. also don’t forget the (Cframe).position
EDIT: I finally know the cause of it, it is because the view model HumanoidRootPart is not anchored so even though the model seems to follow your camera without fails, the physics don’t think so causing the origin of the bullet to goes down with the ViewModel.