How do you make a projectile go into the direction an object is facing?

I’m currently trying to make something very simple.
I want a gun to shoot a projectile where its looking.

The issue is that the projectile isn’t going the way I want it to shoot.
I first used BodyVelocity for this, but then thought that maybe CFrame would be better.

This is what happens

Here is the script I have in the gun:

script.FireGun.OnServerEvent:Connect(function()
	--Create bullet
	local bullet = script.Parent.Muzzle.Bullet:Clone()
	bullet.Parent = workspace
	bullet.Transparency = 0.1
	bullet.Trail.Enabled = true
	bullet.Anchored = true

	--Make bullet go forward
	bullet.Move.Value = true

end)

This is the script I have inside the bullet:

while wait() do
	if script.Parent.Move.Value == true then
		script.Parent.CFrame = CFrame.new(script.Parent.Position - Vector3.new(0,0,1))
	end
end

I hope I can find a solution for this!

5 Likes

You could use Mouse.Hit for the bullet direction

How could I apply this to my gun though?

It’s as simple as 123

local Mouse = game.Players.LocalPlayer:GetMouse()
while wait() do
	if script.Parent.Move.Value == true then
	   script.Parent.CFrame = CFrame.new(script.Parent.Position - Mouse.Hit.Position)
	end
end
1 Like

This doesn’t seem to be working.
I’m using a server script in the bullet. So I tried using events to get the mouse but that didn’t seem to work “[13:29:10.525 - Workspace.Gun.ServerGun:12: attempt to index nil with ‘Hit’]”. Also, I think I’m going to start using BodyVelocity since someone said in my Discord server that CFrame is rather laggy. Might change my mind later though.

You need to send “mouse.Hit” to the server everytime player presses mouse button, so it would update. If you just send mouse to server then it can’t get mouse.Hit since you didn’t sent it it’s Hit value, and since mouse doesn’t exist in server you sending nil to the server. Also if you don’t update mouse.Hit everytime player pressed mouse button then it will be stuck in one direction like you showed in the video. Hope you understood what I said just now.

ShootPart - the part WHERE the bullet comes out from
BodyVelocity - BodyVelocity inside of the bullet

BodyVelocity.Velocity = ShootPart.CFrame.LookVector * Speed

this should work flawlessly

8 Likes

You are making the bullet go to where it’s facing, you didn’t align the rotation for it to shoot through, you could try the guy’s script above this post.

Sorry for the very late reply. I couldn’t respond.
I have a rather, dumb question. What do I put in “Speed”?

Just realized a stupid grammar mistake in the title. Woopsie.

Consider checking out FastCast

Stop.

3 Likes

So, shortly after my dumb question I did some research and “Speed” meant how many studs per second the part would be travelling. I tried your solution out and it didn’t work, this was because of a stupid mistake I made! I accidentally forgot to parent the bodyvelocity to the bullet. Thank you!

I think you should use FastCast module for ranged weapon. Its very good