How do i tilt a direction vector according to an angle

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)

image

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)

Still not it :(( apparently i lack the knowledge of how PointToObjectSpace Works

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)

THANK YOU SO MUCH IT WORKED actually made my day

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