Help with Projectile direction problem

  1. What do you want to achieve?
    I just trying to fire a projectile in the direction my player is looking at.

  2. What is the issue?
    No matter what I do I can’t seem to get the projectile to fire straight. I’m not really sure why. It fires straight down or behind the player. If I Anchor the projectile, it is shows that it is positioned exactly at the humanoid position and facing the correct direction. But does not fly straight forward when I apply the body mover.

  3. What solutions have you tried so far?
    I tried to look this problem up as I know lots of folks have projectiles in their games. But either my brain just isn’t working today or I’m missing something. I’m trying to do this via BodyVelocity body mover.

Here is my code:

-- Fire Missile --
local function fireMissile(player)
	print(player.Name.." Firing Missile["..#missiles.."]")
	
	--Grab one of the missiles and remove from missiles array (pop)
	local missile = table.remove(missiles)
	local newMissile = missile:Clone()
	newMissile.Parent = game.Workspace
	newMissile.Anchored = false
	missile:Destroy()
	newMissile.CFrame = CFrame.new(character.HumanoidRootPart.Position, character.HumanoidRootPart.CFrame.LookVector)
	
	
	--Add Particle Emitter to make a missile trail
	local emitter = Instance.new("ParticleEmitter", newMissile)
	emitter.Color = ColorSequence.new(Color3.new(0,255,0), Color3.new(255,255,255))
	emitter.Size = NumberSequence.new(1, 0)
	emitter.LockedToPart = true
	emitter.VelocityInheritance = 1
	emitter.EmissionDirection = "Back"
	emitter.Lifetime = NumberRange.new(1)
	emitter.Rate = 1000
	emitter.Speed = NumberRange.new(25)
	
	--
	--Do Projectile Code with that missile!!!
	--
	local bodyVelocity = Instance.new("BodyVelocity", newMissile)
	bodyVelocity.Velocity = newMissile.CFrame.LookVector * 50
	newMissile:SetNetworkOwner(nil)
	

	--Destroy that missile (for now)
	newMissile:Destroy()
	
	--Check if we are now out of missiles
	--if so, destory this tool!
	if(#missiles == 0)then missilesTool:Destroy() end
end
missileFireEvent.OnServerEvent:Connect(fireMissile)
2 Likes

You are treating the 2-argument CFrame.new constructor as if it is (position, lookVector), but in fact it works as (position, lookAtPosition). However, in this situation, it would be easier to just set the CFrame of newMissile to equal the CFrame of the HumanoidRootPart directly.

6 Likes

It might be due to you spawning it inside HumanoidRootPart and having CanCollide set to true (by default). Also, I’m not sure if BodyVelocity is the best solution to firing a projectile

1 Like

The missile has canCollide set to off when it is first created.

I am open to suggestions :slightly_smiling_face:

1 Like

You should TOTALLY try this dude!

Seriously, this module is a miracle. If you’re making guns based on the example laser gun presented here you might wanna “transfer” the firing/cast functions from the server script to the local one, but it’s easy. Hope this helps!
PS Happy birthday :V

2 Likes

I tried this but I’m still getting the same issue. I feel like maybe its a problem with the bodyVelocity value. Since when I anchor the projectile it is positioned and facing the correct direction.

1 Like

Well part of my problem was I forgot to wait a few sec before destroying the new missile… Oops. It is still not moving in the right direction though.

1 Like

Try setting the MaxForce on the body velocity; it might be too low.

1 Like

Try
newMissile.CFrame = CFrame.new(character.HumanoidRootPart.Position, character.HumanoidRootPart.Position + character.HumanoidRootPart.CFrame.LookVector)

That way I believe it will face the proper direction.

In your script it says to face the look vector, but that will always will be a unit vector which is positioned at 0,0,0. If you add the position it should be positioned at the player.

Sorry I’m on mobile

All of your answers were helpful thank you. I ended up getting it to work using the Velocity Property in combination with a bodyForce mover to keep the projectile from falling due to gravity. The script below is what I ended up using.

-- Fire Missile --
local function fireMissile(player)
	print(player.Name.." Firing Missile["..#missiles.."]")
	
	--Grab one of the missiles and remove from missiles array (pop)
	local missile = table.remove(missiles)
	local newMissile = missile:Clone()
	newMissile.Parent = game.Workspace
	newMissile.Anchored = false
	missile:Destroy()
	newMissile.CFrame = character.HumanoidRootPart.CFrame
	
	
	--Add Particle Emitter to make a missile trail
	local emitter = Instance.new("ParticleEmitter", newMissile)
	emitter.Color = ColorSequence.new(Color3.new(0,255,0), Color3.new(255,255,255))
	emitter.Size = NumberSequence.new(1, 0)
	emitter.VelocityInheritance = 1
	emitter.EmissionDirection = "Back"
	emitter.Lifetime = NumberRange.new(1)
	emitter.Rate = 1000
	emitter.Speed = NumberRange.new(25)
	
	--
	--Do Projectile Code with that missile!!!
	--
	local bodyForce = Instance.new("BodyForce", newMissile)
	bodyForce.Force = Vector3.new(0,newMissile:GetMass()*game.Workspace.Gravity,0)
	newMissile.Velocity = character.HumanoidRootPart.CFrame.LookVector * 100
	--print(newMissile.Velocity)
	
	wait(5)
	--Destroy that missile (for now)
	newMissile:Destroy()
	
	--Check if we are now out of missiles
	--if so, destory this tool!
	if(#missiles == 0)then missilesTool:Destroy() end
end
missileFireEvent.OnServerEvent:Connect(fireMissile)