# Projectile shooting relative to the axis of the object

I’m trying to make a projectile shoot out of an angle and have the velocity of the relative axis of the gun (Basically have a bullet shoot out of the gun barrel regardless of where it’s facing). However, I have NO idea how to do this. I don’t know too much CFrame and have looked everywhere on the devforum. I tried using CFrames but STILL couldn’t get it to work. I scrapped those scripts because I don’t think they would work at all. I know that there are gun tutorials by AlvinBlox on youtube but that shoots relative to your mouse.

If you can find any tutorials or anything that might help, please feel free to share them. Thanks!

2 Likes

I have no idea what you mean, can you send screenshots or gifs?
Do you mean that the speed of the bullet velocity is slow when the mouse position is near and fast when its far?

1 Like

@Expertsm Lets say your making a cannon. The cannonball needs to shoot out through the end. The direction of the velocity of the cannonball should be defined by the position and rotation of the cannon:

2 Likes
``````local Speed = 100
Cannonball.Velocity = Cannonball.CFrame.LookVector * Speed``````
3 Likes

What is the lookvector @mooneye600?

1 Like

it gives a unit vector where the cframe rotation is pointing.

3 Likes

Thanks @Moonvane, just what I was looking for.

It works nicely when the “cannon” is stationary and pointing in any direction, but what if the cannon is moving?
How could you multiply in the velocity of it so it shoots with stronger force when going forward and with weaker force when backing up?

hmm, that is a very interesting problem- i would do something like this:

``````local pripart = script.Parent.PrimaryPart
local cannonball = script.Parent.CannonBall

local cannonballspeed = 100
local extraSpeedAmount = 0.5 -- the amount of extra speed added

--event ...
local forwardVectorOfTheCannon = pripart.CFrame.LookVector -- making it a variable since it might be different on your cannon

local unit = pripart.Velocity.Magnitude > 0 and pripart.Velocity.Unit or Vector3.new()
local extraSpeed = forwardVectorOfTheCannon:Dot(unit) * pripart.Velocity.Magnitude -- so the extra speed is dependent on which way the cannon is moving, e.g. if it is moving towards the right side, the extra speed will be 0, since the velocity will be going towards the right side (the LookVector is exactly perpendicular to the LookVector, the same rules apply for the LeftVector, and so forth)

cannonball.Velocity = forwardVectorOfTheCannon * cannonballspeed + forwardVectorOfTheCannon * extraSpeed * extraSpeedAmount
``````

using this method, when the cannon is moving forwards, the cannonball will have 100% extra speed, and when it is moving backwards, it will have -100% extra speed, and when it is movign sideways, it will have 0% extra speed.
Hope this helps!

2 Likes

Thank you very much!

I think I managed to solve it (a bit easier?) like this:

``````local fireball = game.ServerStorage.Fireball
local fireballCopy = fireball:Clone()
local car = game.workspace.Jeep
local spawnPoint = car.Body.SpawnPoints.SpawnFront
local speed = 100

local carVelocity = car.DriveSeat.Velocity

fireballCopy.Position = spawnPoint.Position
fireballCopy.Parent = game.Workspace

fireballCopy.Velocity = (spawnPoint.CFrame.LookVector * speed) + carVelocity``````
1 Like