Tween bullet to move in raycast direction?

I simply just want to tween the bullet to move in the raycast direction, but I am unsure of how to do this. this is my current script

local function onClientEvent(muzzle: Attachment, intersection: Vector3, distance: number)
	local cosmeticBullet = ReplicatedStorage.Assets.Effects.CosmeticBulletTemplate:Clone()
	cosmeticBullet.Size = Vector3.new(0.5, 0.5, distance)
	cosmeticBullet.CFrame = CFrame.lookAt(muzzle.WorldPosition, intersection) * CFrame.new(0, 0, -distance / 2)
	cosmeticBullet.Parent = Workspace
-- Tween bullet cframe here?
	task.delay(BULLET_LIFETIME, function()
		cosmeticBullet:Destroy()
	end)
end

If the raycast hits something, tween the bullet from the muzzle to the hit point, if it misses tween the bullet along the raycast’s maximum length.

Correction:
If the bullet doesn’t hit anything set the lifetime to BULLET_LIFETIME, if it hits something calculate the distance from the muzzle to the target and then divide that by the bullet’s max distance to get a percent and then multiply that percent by the bullet’s maximum distance

1 Like

i already coded something that handles that here in my server-side

local intersection = raycastResult and raycastResult.Position or origin + direction

direction is automatically multiplied by the range of the gun, that accoutns for the raycast length

let me just mess around using ur idea to tween it’s position
edit: tried this… but it didnt work, it basically just teleports, doesnt travel at all no matter how much I adjust tween time

TweenService:Create(cosmeticBullet, TweenInfo.new(5), {Position = intersection}):Play()

I’m still confused on what you mean by that

Solved:

local Workspace = game:GetService("Workspace")
local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local function onClientEvent(muzzle: Attachment, intersection: Vector3, distance: number)
	local cosmeticBullet = ReplicatedStorage.Assets.Effects.CosmeticBulletTemplate:Clone()
	cosmeticBullet.CFrame = CFrame.lookAt(muzzle.WorldPosition, intersection)
	cosmeticBullet.Parent = Workspace
	TweenService:Create(cosmeticBullet, TweenInfo.new(distance / 1000), {CFrame = cosmeticBullet.CFrame * CFrame.new(0, 0, -distance / 2)}):Play()
	task.delay(distance / 1000, function()
		cosmeticBullet:Destroy()
	end)
end

ReplicatedStorage.Remotes.VisualizeShot.OnClientEvent:Connect(onClientEvent)

used the previous stuff I did with the cframe in the original code and used that

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.