Shoot part in direction of camera

Hello, so i have this scripts that shoots a part to where the players mouse is, but i want to change it to be in the direction of the camera. How do i do that?

local bullet = game.ReplicatedStorage.FireRelease.GreatFireBall:Clone()
	local FireSound = script.Parent.FlameSound:Clone()
	FireSound.Parent = player.Character.HumanoidRootPart
	player.Character.HumanoidRootPart.FlameSound:Play()
	
	local RockSound = script.Parent.RocksSound:Clone()
	RockSound.Parent = player.Character.HumanoidRootPart
	player.Character.HumanoidRootPart.RocksSound:Play()
	
	bullet.CFrame = CFrame.new(headPos * Vector3.new(0,0,-7), mousePos)
	
	
	local bodyVelocity = Instance.new("BodyVelocity")
	bodyVelocity.Velocity = bullet.CFrame.lookVector * velocity
	bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	bodyVelocity.Parent = bullet

	
	bullet.Parent = workspace
1 Like

Is this what you’re looking for?

local camera = workspace.CurrentCamera
local startPosition = Vector3.new() --startPosition is probably going to be where you shoot the part from

local direction = (startPosition - camera.CFrame.Position).Unit --this is the direction to the camera from startPosition.

I think that is. How would i apply that to my script?

After reading your code it’s probably better for you to do this

local cameraPosition = camera.CFrame.Position --if this is in the server, you're gonna have to get the camera position from the client.
bullet.CFrame = CFrame.lookAt(headPos * Vector3.new(0,0,-7), cameraPosition)

It’s better to use CFrame.lookAt() instead of CFrame.new() when you want something to look at a position.

When shooting from the player, it goes back toward the player

I may have misinterpreted what you wanted, do you want the part to shoot at the camera or shoot where the camera is looking?

Oh im sorry, i wanted it to shoot where the camera is looking, that’s totally my fault

Well then you can just get the camera’s LookVector

camera.CFrame.LookVector

you can find the direction of the camera by doing Camera.CFrame.LookVector, along with the position by Camera.CFrame.Position

For some reason the direction it shoots is off by alot

send you’re current code please

local pos = Character.Head.CFrame.p
	RocksModule.Ground(pos,10,Vector3.new(2,2,2),nil,15,false,3)
	local bullet = game.ReplicatedStorage.FireRelease.GreatFireBall:Clone()
	local FireSound = script.Parent.FlameSound:Clone()
	FireSound.Parent = player.Character.HumanoidRootPart
	player.Character.HumanoidRootPart.FlameSound:Play()
	
	local RockSound = script.Parent.RocksSound:Clone()
	RockSound.Parent = player.Character.HumanoidRootPart
	player.Character.HumanoidRootPart.RocksSound:Play()
	
	
	bullet.CFrame = CFrame.new(headPos * Vector3.new(0,0,-20), cameraPosition)
	
	
	local bodyVelocity = Instance.new("BodyVelocity")
	bodyVelocity.Velocity = bullet.CFrame.lookVector * velocity
	bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
	bodyVelocity.Parent = bullet

	
	bullet.Parent = workspace

Does this work for you?

local startPosition = headPos * Vector3.new(0,0,-20)
bullet.CFrame = CFrame.lookAt(startPosition, startPosition + workspace.CurrentCamera.CFrame.LookVector)

you should make the bullet’s cframe the player’s camera if its a first person game, if you have it at the shoot position then it will originate at the throw position

image
image
or else its just going to look like this

1 Like

So here, i kind of made my own shift lock type of thing, this is how it looks

I dont know if that has anything to do with it

But another really big reason of why im trying to use camera instead of mouse even though mouse works perfectly, is because im also trying to make this system for mobile, but its kind of difficult with mobile and mouse because it gets the spot the player touched last on the screen. But i always want it to be in the middle of the screen for mobile, just like this shift lock system

I tried making another dev forum post about this but no one responded

Does this achieve that?

Not really, it goes in the right direction now, but it goes to far down. This may be because i applied camera offset for my shift lock system

I offseted the camera to be a bit higher than the player so that i could have the mouse on top of the player

Is this dev forum post done? I dont know no ones responding

Ok so anyways, idrk, for those who are looking at this hoping for a resolved issue. I basically had set a camera offset for my costom shift lock system.

local startPosition = headPos * Vector3.new(0,0,-7)
	bullet.CFrame = CFrame.lookAt(startPosition, startPosition + cameraPosition + Vector3.new(-0.01,0,0))

so obviously it was necessary for me to counter that offset by adding an offset to the players look vector, and now it works perfectly :hand_with_index_finger_and_thumb_crossed: