Projectile not flying proper trajectory

Point:
The point of this weapon is to fire a projectile in a straight line from the barrel to whatever is in front of it. Pretty simple I suppose.

Problem:
The projectile seems to be flying downwards even though it’s set up to fly straight out of the barrel. I don’t know if I’m missing something, or overlooking an issue in the code or what. Any Help is greatly appreciated.

I’ve tried changing the CFrame to different things and nothing really seems to solve the issue.

https://gyazo.com/7283f014e9fbe9d918fcaa39a4901fff
https://gyazo.com/2454e083af47054d39d057e5b3ebd420

As you can see, the projectile flies a little below the barrel.

Code:

function module.fireProjectile(car, gun, player)
local damage = damages[gun.Name]
if damage then
	local projectile = script.Asset_Storage.Bullet:Clone()
	parentItem(projectile, damage, player)
	local emitter = projectile.Fins:FindFirstChildWhichIsA("ParticleEmitter") or createEmitter(projectile.Fins)
	
	local bodyForce = Instance.new("BodyForce")
	bodyForce.Force = gun.firePart.CFrame.LookVector * 175
	bodyForce.Parent = projectile.Body1
	emitter.Enabled = true
	projectile.Body1.CFrame = CFrame.new(gun.firePart.Position, gun.AIMING.CFrame.LookVector) * CFrame.Angles(math.rad(-90), 0, 0)
	projectile.Parent = workspace
  end
end

The system is set up pretty simply, there’s a part that moves the barrel and such for aiming, and a firePart and that part is where the projectile will spawn.

image

The blue part is the firePart, and the red one AIMING.

image

The projectile is set up as so, everything is welded to Body1 and they’re all unanchored, and non-collidable MeshParts.

I really don’t know what to do at this point, any help is greatly appreciated. If I left out any missing bits, please let me know.

1 Like

The BodyForce still needs time to accelerate the projectile. Why not set the velocity of the projectile immediately in addition to the BodyForce?

EDIT: Follow up question, but do you want the projectile to fly in a perfectly straight line? If you do, then you can make the body force counteract the force of gravity so that it doesn’t fall at all.

1 Like

Or, you could just use bodyVelocity, if you don’t want gravity to affect anything.

2 Likes

Don’t forget about the new massless setting, either.

So it’s gravity pulling it down?

You are using BodyForce, without adding extra power to the Y axis to counteract the gravity. After watching your video, it seems like your projectile isn’t firing below your barrel, but rather is just falling steeply after launch due to no resistance to gravity besides the Y axis of the initial shot direction. For your purpose, it makes much more sense to use a BodyVelocity, since its MaxForce property can restrict any outward forces (including gravity), making the projectile only be affected by the force YOU exert on it.

1 Like

You construct the projectile’s CFrame in a way that does not point it in the correct direction.

Try

projectile.Body1.CFrame = CFrame.new(gun.firePart.Position, gun.firePart.Position + gun.AIMING.CFrame.LookVector) * CFrame.Angles(math.rad(-90), 0, 0)

The constructor
CFrame.new(vectorA, vectorB)
creates a CFrame at position vectorA, and then points to position vectorB. The way that it is being used in the original code provided is putting a local direction, instead of a world location as vectorB to look at. In turn, it’s always looking at somewhere near the world origin because it is misinterpreting the direction vector as a position vector. In my provided fix, you put the desired direction into the relative space of the starting position, by adding the direction to the starting location.

5 Likes

Couldn’t you just have a single front-facing part, and then just do:

  projectile.Body1.CFrame = Gun.AIMING.CFrame

you could make the Gun.AIMING part have the same position as the blue part, which would make the projectile spawn from inside the barrel as desired.