Weird Shell Ejection

  1. What do you want to achieve?
    A proper directional shell ejection in weapon viewmodels.

  2. What is the issue?
    https://i.gyazo.com/0db650831ef1b9e82a85235aacd11ad0.mp4

  3. What solutions have you tried so far?
    There was something on this, I checked and used some of what was there but I still cannot get it to eject in its proper direction (towards the right)

Shell Ejection Script

		spawn(function()
			local ShellFolder = ReplicatedStorage:WaitForChild("Shells")
			
			for _, GetShell in pairs(ShellFolder:GetChildren()) do
				if GetShell:IsA("BasePart") and GetShell.Name == self.viewmodel.Offsets.ShellType.Value then
					local ShellClone = GetShell:Clone()
					ShellClone.Parent = workspace.Camera
					ShellClone.CFrame = self.viewmodel.Receiver.ShellEjection.CFrame
					local AngularCFrame = CFrame.Angles(math.rad(math.random(-10, 10)), math.rad(math.random(-10, 10)), math.rad(math.random(-10, 10)))
					ShellClone.CFrame = ShellClone.CFrame * AngularCFrame
					ShellClone.Velocity = (self.viewmodel.Receiver.ShellEjection.CFrame.RightVector) * 8 + Vector3.new(10,math.random(8, 23),math.random(-5,5))
					
					game:GetService("Debris"):AddItem(ShellClone, 5)
				end
			end
			
		end)
2 Likes

The reason why its doing this is because the position is in World space. If you change it to object space, It should work. If you dont know what Im taking about, see this, Object and World Space

3 Likes

If you want the answer, I think its this.

ShellClone.Velocity = (self.viewmodel.Receiver.ShellEjection.CFrame.RightVector) * 8 * ShellClone.Cframe * ShellClone.CFrame:PointToObjectSpace(Vector3.new(10,math.random(8, 23),math.random(-5,5)))
2 Likes

Yep, the problem is the X, gotta use the shell ejection CF to make objectspace to world space especially for that second vector3

ShellClone.Velocity = (self.viewmodel.Receiver.ShellEjection.CFrame.RightVector) * 8 * (self.viewmodel.Receiver.ShellEjection.CFrame.Rotation*Vector3.new(10,math.random(8, 23),math.random(-5,5)))
1 Like

Even after adding this, the problem still persists.

1 Like

Yes my code is little incorrect, will try to find a solution

1 Like

Why dont you try this.

ShellClone.Velocity = (self.viewmodel.Receiver.ShellEjection.CFrame.RightVector) * 8

Added and tried it, but it still didn’t fix the problem.

Is the ShellEjection Part welded to the gun?

All parts that have no Motor6D are welded to the receiver of the gun.

Replace it with this

ShellClone.Velocity = (self.viewmodel.Receiver.ShellEjection.CFrame.RightVector) * 8 * self.viewmodel.Receiver.ShellEjection.CFrame:ToWorldSpace(CFrame.new(Vector3.new(10,math.random(8, 23),math.random(-5,5))))

image
Argument #1 and Argument #2 both have an error.

Im currently on vaction now, it will take me some time since I dont have a place to test.

I will reply as soon as I find a solution

ShellClone.CFrame = ShellClone.CFrame * AngularCFrame * CFrame.Angles(math.rad/2,0,0)

Try rotating the bullet in different axis
(Make sure to try the other 2 axis too)

Sometimes frontvector/rightvector/upvector arent actually facing the front or right etc
maybe thats the problem and to fix it maybe you can simply reverse the rightvector with *-1 so it is facing the left.

This might work, x is the problem so making it this should work.

ShellClone.Velocity = (self.viewmodel.Receiver.ShellEjection.CFrame.RightVector) * 8 + Vector3.new(0,math.random(8, 23),math.random(-5,5))

Hi again, I’ve reached back, I’ve replicated your problem and Found a solution, here is a video showcasing it:

Change it to this

ShellClone.Velocity = (self.viewmodel.Receiver.ShellEjection.CFrame.RightVector) * 8 + Vector3.new(0 ,math.random(8, 23), 0)

The X and Z are causing the problem. So making them to 0 works. Hope it helps :grinning: :grinning: :grinning:

1 Like

Added it but it still didn’t work.

I’m gonna try to redo it and hope it gets fixed.