I am currently making a viewmodel gun system and the swaying is like Insurgency Sandstorm, sort off, so ur gun points in the direction u rotated ur camera to, heres an example of what I mean and what my issue is:
External MediaAs you can see the bullet is going up, not front of the muzzle , which is what I am trying to achieve. How would I fix that? I am not a expert in FastCast or Raycast.
Local code:
Tool.Activated:Connect(function()
if eqp and not Equipping and not Reloading then
if RoundsInMag > 0 and not FireDB then
FireDB = true
script:WaitForChild("RemoteEvent2"):FireServer("ShowEffect")
MouseEvent:FireServer(VM:WaitForChild("GunModel"):WaitForChild("Handle"):WaitForChild("Muzzle").WorldPosition - VM:WaitForChild("GunModel"):WaitForChild("Handle").Position, VM:WaitForChild("GunModel"):WaitForChild("Handle"):WaitForChild("Muzzle").WorldPosition)
--MouseEvent:FireServer(Mouse.Hit.Position, VM:WaitForChild("GunModel"):WaitForChild("Handle"):WaitForChild("Muzzle").WorldPosition)
VM_Fire:Play()
Fire:Play()
RoundsInMag = RoundsInMag - 1
VM:WaitForChild("GunModel"):WaitForChild("Handle"):WaitForChild("Fire"):Play()
script:WaitForChild("RemoteEvent2"):FireServer("PlaySound", Tool:WaitForChild("Handle"):WaitForChild("Fire"))
print(Player.Name.."'s "..Tool.Name.." has "..RoundsInMag.." rounds left in the mag.")
showeffect()
Spread.Shoot()
recoil()
task.wait(FireRate)
FireDB = false
else
VM:WaitForChild("GunModel"):WaitForChild("Handle"):WaitForChild("LowAmmo"):Play()
script:WaitForChild("RemoteEvent2"):FireServer("PlaySound", Tool:WaitForChild("Handle"):WaitForChild("LowAmmo"))
end
end
end)
Server:
function Fire(direction, att)
-- Called when we want to fire the gun.
if Tool.Parent:IsA("Backpack") then return end -- Can't fire if it's not equipped.
-- Note: Above isn't in the event as it will prevent the CanFire value from being set as needed.
-- UPD. 11 JUNE 2019 - Add support for random angles.
local directionalCF = CFrame.new(Vector3.new(), direction)
-- Now, we can use CFrame orientation to our advantage.
-- Overwrite the existing Direction value.
local direction = (directionalCF * CFrame.fromOrientation(0, 0, RNG:NextNumber(0, TAU)) * CFrame.fromOrientation(math.rad(RNG:NextNumber(0, 0)), 0, 0)).LookVector
-- UPDATE V6: Proper bullet velocity!
-- IF YOU DON'T WANT YOUR BULLETS MOVING WITH YOUR CHARACTER, REMOVE THE THREE LINES OF CODE BELOW THIS COMMENT.
-- Requested by https://www.roblox.com/users/898618/profile/
-- We need to make sure the bullet inherits the velocity of the gun as it fires, just like in real life.
local humanoidRootPart = Tool.Parent:WaitForChild("HumanoidRootPart", 1) -- Add a timeout to this.
local myMovementSpeed = humanoidRootPart.Velocity -- To do: It may be better to get this value on the clientside since the server will see this value differently due to ping and such.
local modifiedBulletSpeed = (direction * BULLET_SPEED)-- + myMovementSpeed -- We multiply our direction unit by the bullet speed. This creates a Vector3 version of the bullet's velocity at the given speed. We then add MyMovementSpeed to add our body's motion to the velocity.
if PIERCE_DEMO then
CastBehavior.CanPierceFunction = CanRayPierce
end
local simBullet = Caster:Fire(att, direction, modifiedBulletSpeed, CastBehavior)
-- Optionally use some methods on simBullet here if applicable.
-- Play the sound
end
MouseEvent.OnServerEvent:Connect(function (clientThatFired, mousePoint, attachmentvector)
if not CanFire then
return
end
CanFire = false
local mouseDirection = (mousePoint - FirePointObject.WorldPosition).Unit
for i = 1, BULLETS_PER_SHOT do
Fire(mousePoint, attachmentvector)
end
if FIRE_DELAY > 0.03 then wait(FIRE_DELAY) end
CanFire = true
end)