EDIT: I have figured out how to do it. I moved my invisible part up to compensate for the bullet drop and multiplied the CFrame (with only it's rotation by subtracting the Vector3 value of the CFrame from the whole CFrame) by the front face using Vector3.fromNormalId so the direction would be the face that's pointing to the right of the gun. The script is below:
local bullet = b:Clone() bullet.Parent = workspace bullet.CFrame = CFrame.new(Tool.Eject.Position,workspace.CurrentCamera.CoordinateFrame.p) local direction = (Tool.Eject.CFrame - Tool.Eject.CFrame.p) * Vector3.FromNormalId(Enum.NormalId.Front) bullet.Velocity = direction * 10 game.Debris:AddItem(bullet,1)
Apologies for the vague title, I was not too sure how to word it.
Hi guys, I have a gun with all the parts inside of the gun welded to each other. I'm trying to make bullet shells eject from an invisible part on my gun. My current snippet of code so far for my shell ejection is this:
local bullet = b:Clone() --b is a variable I made earlier on in the full script that references a part. bullet.Parent = workspace bullet.CFrame = CFrame.new(Tool.Eject.Position,workspace.CurrentCamera.CoordinateFrame.p) --The shells come from the Eject part in the tool. bullet.Velocity = Vector3.new(10,5,0) * bullet:GetMass() * 192.6 --Using this to propel the bullet shells to the right of the gun and up in the air without it dropping too fast. bullet.BodyAngularVelocity.angularvelocity = Vector3.new(math.random(),math.random(),math.random()) --Trying to make the bullet spin around randomly in the air, working on this later. game.Debris:AddItem(bullet,1)
The velocity works fine and dandy when I'm facing straight ahead where I spawn. However, if I move my camera around (I'm in first person by the way and I have used code to make my neck move in relation to my mouse, welding my arms to the head and offsetting them appropriately), the velocity of the bullet shells go where I don't want it to go.
For example: If I turned my character horizontally 90 degrees to the left, the bullet shells would fly to the back of the gun. Another 90 degrees and the bullet shells would fly to the left - and so on. If I moved my camera down to make my gun face the floor, the shells would fly way too high - and vice versa.
I was wondering how I could set the velocity of the bullet shells in relation to another part's position, for example, my eject part, so that the shells of my gun always fly out of the right side of the eject part of my gun.
Again, I apologize if I'm too vague on what I'm trying to do. I've not worked with these types of things in quite a while. Any help is appreciated, thank you!
You can solve this by using CFrame.lookVector
. The lookVector returns a unit vector in the direction that the object is facing. So if you wanted to get the direction of a part, you would do this:
local Part = game.Workspace.Part local Direction = Part.CFrame.lookVector
If you wanted to make a bullet travel in the direction that an object is facing, you would do this:
Bullet.Velocity = InvisiblePart.CFrame.lookVector * Speed
Because lookVector returns a vector, it can be multiplied. This means that you can change the speed of the bullet by multiplying the lookVector by a number. So for example if you multiplied the lookVector by 100, the bullet would travel at a speed of 100 studs per second.
So in your script, you would want to add a part where you multiply the velocity by the lookVector, like so:
--Change this: bullet.Velocity = Vector3.new(10,5,0) * bullet:GetMass() * 192.6 --to this: bullet.Velocity = InvisiblePart.CFrame.lookVector * Vector3.new(10,5,0) * bullet:GetMass() * 192.6 --Change "InvisiblePart" to the part that you want the bullets to be in the direction of
Hope this helped!
EDIT: I believe that the reason the bullet still doesn't go straight is because you're multiplying the lookVector by Vector3.new(10,5,0)
. Try getting rid of that vector and it should work.
Also, why are you setting the velocity of the bullet as the mass of the bullet? If you're trying to counter the mass of the bullet so that it doesn't fall, use a bodyvelocity
instead. A bodyvelocity
will keep an object going at a set velocity without falling. Use it like so:
local BV = Instance.new("BodyVelocity") BV.maxForce = Vector3.new(math.huge, math.huge, math.huge) --"math.huge" returns infinity. By setting the maxForce property of the bullet to math.huge, it ensures that the bullet will travel the same speed no matter what the mass of the bullet is. BV.velocity = (InvisiblePart.CFrame.lookVector * [Insert Speed Here]) + Vector3.new(0, 0.15, 0) --You have to add Vector3.new(0,0.15,0) to the velocity because for some reason, even with a velocity of (0,0,0), the bullet still slowly falls. The (0,0.15,0) counters that fall and keeps it stable BV.Parent = Bullet
Hope this helped!