#lock position of object

1 messages · Page 1 of 1 (latest)

lost crag
#

So I made a shell case effect that spawns a physical object when the player shoots. Though, if the player moves in a lot of directions, the casings may go through the gun and whatnot. How can I prevent this?

local function createVFX()
    currentViewmodel = game.Workspace.Camera:FindFirstChildOfClass("Model") :: Model
    if currentViewmodel then
        local ejection = currentViewmodel:FindFirstChild("Bolt"):FindFirstChildOfClass("Attachment") :: Attachment
        local casingClone = gunInfo["Casing Type"]:Clone()
        local muzzle = currentViewmodel:FindFirstChild("Barrel"):FindFirstChildOfClass("Attachment") :: Attachment
        local vfx_muzzleFlash = muzzle:FindFirstChildOfClass("ParticleEmitter") :: ParticleEmitter
        local vfx_light = muzzle:FindFirstChildOfClass("PointLight") :: PointLight

        vfx_muzzleFlash.Enabled = true
        vfx_light.Enabled = true

        task.wait(gunInfo["Fire Rate"])

        vfx_muzzleFlash.Enabled = false
        vfx_light.Enabled = false

        casingClone.Anchored = false
        casingClone.Transparency = 1
        casingClone.Parent = game.Workspace.BulletCasings
        casingClone.CFrame = ejection.WorldCFrame
        casingClone.Rotation = Vector3.new(math.random(20, 180), math.random(20, 180), math.random(20, 180))

        local rng = Random.new()
        local mass = casingClone:GetMass()
        local upVector = ejection.WorldCFrame.UpVector
        local rightVector = ejection.WorldCFrame.RightVector

        local desiredSpeed = upVector * rng:NextNumber(5, 10) + rightVector * rng:NextNumber(10, 15)
        local impulse = mass * desiredSpeed --| Force = mass * acceleration 

        casingClone:ApplyImpulse(impulse)
        task.wait(0.03)
        casingClone.Transparency = 0
    end
end
jade light
#

Use CollisionGroups to avoid collisions with the weapon/player
This way, shell casings don't collide with the body/weapon when they exit.


-- This should be run only once.
local function

 setupCollisionGroup()
    if not pcall(function() PhysicsService:CreateCollisionGroup("Casings") end) then return end
    PhysicsService:CollisionGroupSetCollidable("Casings", "Default", true)
    PhysicsService:CollisionGroupSetCollidable("Casings", "Players", false)
end ````
#

and when cloning the casing

#

PhysicsService:SetPartCollisionGroup(casingClone, "Casings")

#

Move the casing slightly out of the gun before firing it

casingClone.CFrame = ejection.WorldCFrame * CFrame.new(0, 0.1, 0) -- Shift a little

#

Use Debris:AddItem and reduces physics after a while

game:GetService("Debris"):AddItem(casingClone, 5) --It is deleted after 5 seconds, (as many seconds as you want)

#

Create a small wait before making the cap visible

casingClone.CFrame = ejection.WorldCFrame * CFrame.new(0, 0.1, 0)
casingClone.Parent = workspace.BulletCasings

task.wait(0.02)
casingClone.Anchored = false
casingClone.Transparency = 0
casingClone:ApplyImpulse(impulse)````
#

@lost crag Let me know if it works, sorry if I expressed myself badly, I don't speak English

lost crag
#

thanks for chipping in : )