I have a simple question involving moving a part. I am trying to make a gun eject a spent casing, so I need the part to fly up and to the right a bit. I originally used Part.Velocity, but the vector3 value applies relative to the world space, not to the gun itself (facing the gun different directions will cause it to still fly in the same direction). I think body movers would be helpful, but I can’t seem to understand how they work and it might not even be the best option to begin with. If anyone has ideas about how I could achieve this, I would like to know. Thanks!
You need to use CFrame.Angles()
Yes you can use BodyVelocity with a script like this:
BodyVelocity.Velocity = gun.CFrame * CFrame.Angles(math.rad(valueX), math.rad(valueY), math.rad(valueZ))
Try translating from gunCFrame.RightVector to get your desired result, or even use an attachment inside of the gun and spawn your casings there, and send them in the same direction that you face the attachment.
so this will make the velocity change along with the gun being turned? (so that it keeps flinging the same direction)
Yes the CFrame is a combination of the Vector3 position and the orientation so rotating the gun will also change the CFrame and the bullet will be shot proportionally.
well my issue was how exactly to make the casing move in the first place. The velocity property applies the vector3 values as the unchanging positions in the world, so if I look in different directions it wouldn’t work.
so this did not work, output said it was expecting vector3 value for the velocity property but got a cframe.
Oh i’m sorry i forgot to type a property
BodyVelocity.Velocity = gun.CFrame.lookVector * CFrame.Angles(math.rad(valueX), math.rad(valueY), math.rad(valueZ)) * velocity --Velocity is the speed that the part will move at
.lookVector basically gets the direction in which the part is facing
what is that last * velocity at the end referring to? like which part’s velocity is it?
it will multiply gun.CFrame.lookVector * CFrame.Angles(math.rad(valueX), math.rad(valueY), math.rad(valueZ))
by a number, the highest it is more speed will be given to the part
To Launch a part use the Method BasePart:ApplyInpulse(Vector3 Force)
Documentation:
https://developer.roblox.com/en-us/api-reference/function/BasePart/ApplyImpulse
If you want accurate Velocity then you will need tp use BasePart.AssemblyLinearVelocity
and change that.
oh ok, so I can put any number in there and change it to affect the force?
Yes, if you want the part to move really fast put a big number
this looks promising, but I need the vector3 velocity to adapt when the player turns and stay relative to the gun. This seems just like the part.velocity property I was using before which didn’t work.
it is still giving me the same error. I think your code is returning a cframe value instead of vector3 still.
edit: my code is this:
case.CFrame.lookVector * CFrame.Angles(math.rad(20), math.rad(20), math.rad(0)) * 50```
Okay, I think I misread your problem sorry about that.
Velocity is a Vector. So, we can create one by having a direction and a scalar product which is magnitude.
You can get the direction by simply looking up at the CFrame.LookVector
of your HumanoidRootPart which should always face forward.
local Direction = HumanoidRootPart.CFrame.LookVector
Our Scalar should be the speed of the Object in Studs per second
local SPEED = 100
Simply multiplying them together will give us a Velocity
local Velocity = Direction * SPEED
Which we can apply to our Object
Object.AssemblyLinearVelocity = Velocity
Its pretty simple thing to grasp and it comes in very handy for other things too.
Also, if you were working in 2d or if you for some reason only had the angle instead of a CFrame you can simply convert from Polar to Cartesian and then you will end up with your Velocity.
This looks good, but this will only launch in the given direction. I need it to simply face in a direction, but put force on a certain vector3 direction
Here is what I did for my guns I’m scripting
local shell = Chamber.Cartridge:Clone()
shell.Parent = workspace.Debre
shell.Transparency = 0
shell.qCFrameWeldThingy:Destroy()
shell.qRelativeCFrameWeldValue:Destroy()
--launch shell out wards
local HumanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")
local Direction = HumanoidRootPart.CFrame.RightVector
local SPEED = 20
local Velocity = Direction * SPEED
shell.AssemblyLinearVelocity = Velocity
shell.Anchored = false
I cloned the cartridge that’s in the tool, made it visible and destroyed all its welds. Then launched It to the right of the players camera.