Giving a part velocity + velocity of another part

I am trying to make a FPS framework, and I’m currently making a casing eject system. So far it works, but I wish that the casings would move not just in the direction given to it but also in the direction of player’s movement. I’m using VectorForce to give the casing velocity.

local function dropcasing(v)
	local Casing = v.Casing.Value:Clone() -- the casing
	local Velocity = v.Vel.Value -- velocity of the casing without the player's velocity being applied
	Casing.CFrame = v.CFrame -- v is the part from which the casing is ejected
	local Att = Instance.new("Attachment",Casing)
	local Force = Instance.new("VectorForce",Casing)
	Force.RelativeTo = "Attachment1"
	Force.Attachment1 = v.Attachment
	Force.Attachment0 = Att
	Force.ApplyAtCenterOfMass = true
	Force.Force = Velocity
	task.wait()
	Force:Destroy()
end

So far, I’ve tried getting the player’s movement direction relative of the player’s HRP rotation, multiplying it by casing velocity, and adding it to casing velocity, like this

local Velocity = v.Vel.Value + v.Vel.Value * Character.HumanoidRootPart.CFrame:VectorToObjectSpace(Character.Humanoid.MoveDirection)

The velocity for the casing is (1,1,0), if that helps

So just velocity inheritance? All you need to do is add the player’s velocity to the casing velocity and that’s it

local Velocity = v.Vel.Value + Character.HumanoidRootPart.AssemblyLinearVelocity

I’ve tried that, but that just sends the gun casing flying, I’m guessing that could be because of the difference in mass between the player and the casing.

Actually no, normalize the entire resultant vector, so that it turns into a “net direction” vector

local Velocity = (v.Vel.Value + Character.HumanoidRootPart.AssemblyLinearVelocity).Magnitude

Then you can multiply the velocity by whatever you want right before applying it to the VectorForce

Same issue again, it just makes the velocity too high and launches the casing very far and very fast

I made a mistake you’re supposed to do .Unit not .Magnitude, but you probably ignored anyways it since it would otherwise throw an error

It should actually be weaker than the original velocity you have since (1, 1, 0) has a length of sqrt(2) while a normalized vector always has a length of 1

Why use VectorForce? Vector force gives off force which gives acceleration which is not what you intended.

Why not set the casings AssemblyLinearVelocity or :ApplyImpulse?

I used VectorForce as it makes applying force relative to the part’s orientation easier

If that is the case, then I believe the better solution is to use the parts.CFrame.RightVector property instead to find this relative direction.

Attachments also have a .WorldCFrame property you can use.

Okay, I ended up fixing the issue. What I did was I took the player’s HRP’s velocity and multiplied it by the mass of the casing being ejected, to get the force I needed to apply to the casing to get the same velocity as the player (F = M*V/T).

local Velocity1 = Character.HumanoidRootPart.AssemblyLinearVelocity -- Player's HRP velocity
local Velocity2 = Vector3.new(8,15,0) -- Velocity that had to be applied to the empty casing
local Force = (Casing.CFrame:VectorToWorldSpace(Velocity2) + Velocity1) * Casing.Mass -- The force that has to be applied to the casing
Casing:ApplyImpulse(Force)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.