Beam Tracer. Where Should i Start?

How would i go into making a beam into a tracer for a gun?

so far i have:

local beam = ReplicatedStorage.Tracer:Clone()
beam.Part.Attachment.Position = hitFromPos
beam.Part.Attachment1.Position = Target
	
game:GetService("Debris"):AddItem(beam, 0.1)

hitFromPos is the position of the barrel
target is the position of the place i am shooting at (Mouse.hit.p)

here is the tracer: tracer.rbxm (3.3 KB)

1 Like

You can use RayCasting for this! Ray casting allows you to shoot a ray from the barrel to where the player clicks. Then you can get the part that the ray hit as well as the position the ray hit.

1 Like

i have a raycast already… what i want to know is how can i make the ray show by using a beam (i was using a part before and that was ugly)

I see! The article I linked shows how you can do that! You need to position the beam half way in between the origin and hit point of the ray (you can average two positions to get the center like this (pos1 + pos2)/2!), scale its Z (forward/backward) size to the distance (magnitude) between the two points, and point it towards the hit point (or origin) of the ray.

1 Like

i was going to do it by placing the attachment it is linked to at the place i am shooting at. if you saw in the model i sent, it is a beam not a part. the part is just a holder for the attachments

I see! I misunderstood. I also cannot look at your file at the moment but I might later.

To do this you would need to change the attachment position. The Beam documentation has an excellent visual of how the Beam constraints work.

You would want to set the Attachment0 position to the origin position of the ray, the Attachment1 position to the position that was hit, and the CurveSizes to the height for each half of the curve, or 0 if you don’t want any curving.

1 Like

like this?

beam.Beam.Attachment0.Position = hitFromPos
beam.Beam.Attachment1.Position = Target

That’s close but you’d probably want to use WorldPosition instead of Position since Position will offset relative to the beam part’s position. If the beam part’s position is 0,0,0 this will work as intended though. Make sure Target and hitFromPos are both Vector3 values too!

would i parent it to the workspace while it is being shown?

Yep! You’d need the beam part that has the beam to be in the workspace. You can also make the beam part the tool’s handle if you wanted to (although I don’t know how WorldPosition behaves this way so I don’t know if moving the character will cause problems).

only problem i need to fix now is the sever lag results in the beam floating if the player is moving

Are you trying to make the beam connect to the handle of the tool and follow it? If you are, you could place the Attachment1 of the beam in the hit part. That will cause Attachment1 to move relative to the hit part. And the Attachment0 could be in the tool’s handle so it moves relative to the handle.

nvm, i fixed it. was just the wait i put in for testing

1 Like