Help with tweening a raycast bullet in a single direction for effect

I’m working on a new third-person weapon system and I’m going for a new bullet look, I’ve been playing the game Eclipsis and I absolutely love the bullet travel style they use and I’m interested in trying to do something like it, though I cannot seem to figure out how to tween the bullet (test part) in a single direction and it comes out completely incorrect. I’m still learning CFrame, Size, and Position with TweenService so this is more on the difficult side for me. Any help is appreciated. (I’ve already finished the weapon system, this is purely for effect)

Here is what my test effect looks like:

https://gyazo.com/65fd08af0f18742c0e1f1394db93f523

local p = workspace.Part
local ts = game:GetService("TweenService")
while wait(0.6) do
	ts:Create(p,TweenInfo.new(0.5),{Size = Vector3.new(p.Size.X - 100,p.Size.Y,p.Size.Z)}):Play()
	wait(0.6)
	ts:Create(p,TweenInfo.new(0.5),{Size = Vector3.new(p.Size.X + 100,p.Size.Y,p.Size.Z)}):Play()
end

I am trying to get the bullet to tween to a single point size-wise, like this:

https://gyazo.com/1bcf155890d8702183d2598f1dd22b6c

What you need first is to obtain the point of origin and the point of target, your origin being the end of the gun barrel and the target being the point of your mouse hit.
Then you will need to cast a ray from the origin to the target
Lastly you place the bullet between the origin (end of gun barrel) and the ray hit position.

Let me help you with a demo,
If you put 3 parts in workspace and call them origin, target and bullet respectively and put a cylindermesh in the bullet you can start off with this piece of code:

origin = game.Workspace.origin
target = game.Workspace.target
bullet = game.Workspace.bullet
ignore = {} -- ignore list, put whatever you need in here that needs to be ignored
ray = Ray.new(origin.CFrame.p,(target.CFrame.p - origin.CFrame.p).unit * 150) -- initialize ray class from origin to target (the 150 is the max. range of the ray)
hit, position = game.Workspace:FindPartOnRayWithIgnoreList(ray, ignore) -- cast the ray (hit = part that the ray hit, position = the position of where the ray hit)

distance = (origin.Position-position).Magnitude
bullet.Size = Vector3.new(distance,0.5,0.5) 
bullet.CFrame = CFrame.new(origin.Position,target.Position)*CFrame.new(0,0,-distance/2)*CFrame.Angles(0,math.rad(90),0) -- angles is to correct the orientation for the cylinder mesh

After that you should shrink the bullet.
I hope you can get started with this.

1 Like

Thank you for your reply! I will try this out.

1 Like