I’m trying to make A recoil System the direction is as in every gun system which is mouse.Hit.Position - Muzzle.Position but i’m trying to tilt the direction upwards here’s what i tried
fire.OnServerEvent:Connect(function(player,mouse,muzzle,Stamp)
local raycastParams = RaycastParams.new()
local char = player.Character or player.CharacterAdded:Wait()
raycastParams.FilterDescendantsInstances ={char,muzzle.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local mouse = muzzle.CFrame*CFrame.Angles(math.rad(20),0,0):VectorToObjectSpace(mouse)
local raycastResult = workspace:Raycast(muzzle.Position,muzzle.CFrame*CFrame.Angles(0,0,0):VectorToWorldSpace(mouse)*2500, raycastParams)
if raycastResult then local bc=bullet:Clone()bc.Parent,bc.CFrame,bc.Anchored =game.Workspace.Bullets,CFrame.new(raycastResult.Position),true
end end)
I think the problem is that you’re rotating muzzle’s cframe by 20 radians and not 20 degrees
To convert 20 degrees to radians do math.rad(20)
Here’s the updated code
fire.OnServerEvent:Connect(function(player,mouse,muzzle,Stamp)
local raycastParams = RaycastParams.new()
local char = player.Character or player.CharacterAdded:Wait()
raycastParams.FilterDescendantsInstances ={char,muzzle.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local mouse = muzzle.CFrame*CFrame.Angles(math.rad(20),0,0):VectorToObjectSpace(mouse)
local raycastResult = workspace:Raycast(muzzle.Position,muzzle.CFrame*CFrame.Angles(0,0,0):VectorToWorldSpace(mouse)*2500, raycastParams)
if raycastResult then local bc=bullet:Clone()bc.Parent,bc.CFrame,bc.Anchored =game.Workspace.Bullets,CFrame.new(raycastResult.Position),true
end end)
I think where you’re messing up is on this line: local mouse = muzzle.CFrame*CFrame.Angles(20,0,0):VectorToObjectSpace(mouse)
CFrame.Angles takes the angle in radians, but you set it to degrees without converting so when you did CFrame.Angles(20, 0, 0) it’s actually setting it to 1145.92 degrees on the X axis. Change the code to this: CFrame.Angles(math.rad(20), 0, 0)
To tilt a direction vector upwards you can create a cframe that is looking in that direction
with this line of code
CFrame.lookAt(Vector3.zero, direction)
and then you can tilt it upwards by multiplying it by a CFrame that is rotated on the X axis
then to get the vector direction of the CFrame you can get the LookVector of the CFrame
Here’s the whole code:
fire.OnServerEvent:Connect(function(player,mouse,muzzle,Stamp)
local raycastParams = RaycastParams.new()
local char = player.Character or player.CharacterAdded:Wait()
raycastParams.FilterDescendantsInstances ={char,muzzle.Parent}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local directionCFrame = CFrame.lookAt(Vector3.zero, mouse - muzzle.CFrame.Position)
local mouse = (directionCFrame * CFrame.fromOrientation(math.rad(20), 0, 0)).LookVector
local raycastResult = workspace:Raycast(muzzle.Position,mouse*2500, raycastParams)
if raycastResult then local bc=bullet:Clone()bc.Parent,bc.CFrame,bc.Anchored =game.Workspace.Bullets,CFrame.new(raycastResult.Position),true
end end)
Also if the direction is tilted downwards then change math.rad(20) to -math.rad(20)