Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to set part's velocity in relation to another part's position? [SOLVED]

Asked by 9 years ago

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!

0
Why not just make the invisible part face the correct direction, then use lookVector? Perci1 4988 — 9y
0
The invisible part does face the right direction. It's just that I don't know how to make the velocity of my part relative to the position of the invisible part. Spongocardo 1991 — 9y
0
I edited my answer TurboFusion 1821 — 9y

1 answer

Log in to vote
1
Answered by 9 years ago

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!

0
Thank you for this detailed answer. I tried it at first but the bullets kept flying downwards. I then realized the front face of my invisible part was not facing off the right but was facing downwards (which explains why the bullets were flying down.) So, I tilted and rotated the part and now my shells eject correctly out of the gun. Thank you for shedding some light on lookVector for me. :) Spongocardo 1991 — 9y
0
Hold on, scratch that. It did work but I get the same issue that I did before where if I turn a certain way, the bullets don't go in the right direction. However, if I face behind myself, instead of it going left, it goes to the right, which is where I want the bullets to fly out. Spongocardo 1991 — 9y
0
Thanks for the edit. I do want the bullets to fall to give it a realistic effect. Also, I don't want the bullets to go straight, I want the bullets to go to the right and go up a bit from my invisible part via velocity, which is why I'm multiplying by that vector (to try and make it go to the right and up instead of forward, which it only does when my character is at a certain position.) Spongocardo 1991 — 9y
0
I was able to figure it out myself, but I will accept your answer as you gave me a starting point to work from. Spongocardo 1991 — 9y
Ad

Answer this question