How to make a part points to direction that camera is facing?

(sorry for my english i’m using a translator, some things might not be clear, just ask me and i clarify them)

Hello, i’m working on a first person gun without fake arms, the camera works pretty well but the gun doesn’t point towards camera, i searched a bit in devforum and found this:

local WeldOffset = HumanoidRootPart.CFrame:Inverse() * Torso.CFrame

local weld = Instance.new("Weld")

weld.Part0 = HumanoidRootPart
weld.Part1 = Torso
weld.Parent = Character

RunService.Heartbeat:Connect( function()
	
	local ReferenceCFrame = CFrame.new(HumanoidRootPart.Position, Mouse.Hit.p)
	
	weld.C0 = WeldOffset * HumanoidRootPart.CFrame:toObjectSpace(ReferenceCFrame)
end)

This worked, but this script uses (position, position), my gun uses raycast, which uses (position, direction) so sometimes the gun gets too close from the camera (depending of the distance of the mouse hit p):

What i want to achieve is instead of using mouse to change the “orientation” of the root part i want to use the direction that camera is facing

Are you talking about the way the gun is positioned up a little bit when you aim towards the wall?

Yes, that happens when i use the code that i mentioned edit: in the rest of the video you can see that isn’t “a little bit”

Gotcha, from what I’m assuming you’re using the Players’ Mouse, you can use Mouse.TargetFilter to ignore any other parts you want the Mouse to look at apart from the Camera itself, which should position the Gun to it’s middle position

Put those Parts (Or walls) inside a Folder that’ll ignore all the descendants of those objects inside that Folder, and I think it should work:

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

Mouse.TargetFilter = workspace.IgnoreParts --This will be your Folder

local WeldOffset = HumanoidRootPart.CFrame:Inverse() * Torso.CFrame

local weld = Instance.new("Weld")

weld.Part0 = HumanoidRootPart
weld.Part1 = Torso
weld.Parent = Character

RunService.Heartbeat:Connect( function()
	
	local ReferenceCFrame = CFrame.new(HumanoidRootPart.Position, Mouse.Hit.p)
	
	weld.C0 = WeldOffset * HumanoidRootPart.CFrame:toObjectSpace(ReferenceCFrame)
end)

Try that and see if anything changes

1 Like

I also cannot see the problem from the vid.

The gun is lower when aiming into space because mouse returns a spot on the Skybox or not(If a smaller ray).

You didn’t show code; i don’t know which way you are trying to convert, but ToDirection conversion is):

(Position1 - Position2).unit – I think

Need more info…

Oh you almost made it, sadly, the ray that my gun casts uses mouse.hit.p so if i filter these parts in the folder they also are ignored by the ray because the target filter is set in the player mouse, not just inside the script.

edit: if you don’t understood, that’s the line of code that casts the ray:

local Raycast = game.Workspace:Raycast(FirePart.Position, CFrame.new(FirePart.Position, MouseHitp).LookVector * 100, Params)

I want the gun to be lower all the time edit: when i aim in walls the gun gets in the way of the mouse that’s the issue

What i want to achieve is setting the orientation of a part with camera.CFrame.LookVector

The Camera has a CFrame, so it has a CFrame.LookVector.

You need a

Workspace.CurrentCamera

to get it though…

I already have it, and i know how to get it.

image

I think you’re not understanding, what i don’t know is how to set a part orientation with this look vector like:

part.Orientation = Camera.CFrame.LookVector -- But this doesn't works
local RunService = game:GetService("RunService")
local Uis = game:GetService("UserInputService")

local RemoteEvent = game.ReplicatedStorage.ChangeRootCFrame
local Event = game.ReplicatedStorage.ChangeCameraCFrame

local Player = script.Parent.Parent
local Character = script.Parent.Parent.CharacterAdded:Wait()
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
local Torso = Character:WaitForChild("Torso")
local Head = Character:WaitForChild("Head")
local RightArm = Character:WaitForChild("Right Arm")

local Mouse = Player:GetMouse()

local Camera = game.Workspace.CurrentCamera
Camera.CameraType = Enum.CameraType.Scriptable

local default_CameraInstance = Head
local default_CameraRotationX = 0
local default_CameraRotationY = 0
local default_CameraZoom = 1

local cameraInstance = default_CameraInstance
local cameraPosition
local cameraRotationX = default_CameraRotationX
local cameraRotationY = default_CameraRotationY
local cameraZoom = default_CameraZoom

local cameraMouseRotateSpeed = 0.25

-- Camera functions --

RunService.RenderStepped:Connect( function()
	
	for _, v in pairs(Character:GetChildren()) do -- Makes arms visible in first camera
		
		if v:IsA("Part") and v.Name ~= "Right Arm" and v.Name ~= "Left Arm" then
			
			v.LocalTransparencyModifier = 1
			
		elseif v.Name == "Right Arm" or v.Name == "Left Arm" then
			
			v.LocalTransparencyModifier = 0
		end
	end
	
	if Uis.MouseBehavior ~= Enum.MouseBehavior.LockCenter then -- Locks mouse
		
		Uis.MouseBehavior = Enum.MouseBehavior.LockCenter
	end
	
	cameraPosition = cameraInstance.Position
	
	local cameraRotationCFrame = CFrame.Angles(0, cameraRotationX, 0) * CFrame.Angles(cameraRotationY, 0, 0)
	Camera.CFrame = cameraRotationCFrame + cameraPosition + cameraRotationCFrame * Vector3.new(0, 0, cameraZoom)
end)

Event.Event:Connect( function(NeedChange, AimPart)
	
	if NeedChange then
		
		cameraInstance = AimPart
		cameraZoom = 0
		
	else
		
		cameraInstance = default_CameraInstance
		cameraZoom = default_CameraZoom
	end
end)

local function UpdateCamera()
	
	local Delta = Uis:GetMouseDelta()
	
	cameraRotationX = cameraRotationX - Delta.X * math.rad(cameraMouseRotateSpeed)
	
	if (cameraRotationY - Delta.Y * math.rad(cameraMouseRotateSpeed)) < 1.3 and (cameraRotationY - Delta.Y * math.rad(cameraMouseRotateSpeed)) > -1.5 then
		
		cameraRotationY = cameraRotationY - Delta.Y * math.rad(cameraMouseRotateSpeed)
	end
end

RunService:BindToRenderStep("MouseMovement", Enum.RenderPriority.Input.Value, UpdateCamera)

-- Root part to mouse functions --

local WeldOffset = HumanoidRootPart.CFrame:Inverse() * Torso.CFrame

local weld = Instance.new("Weld")

weld.Part0 = HumanoidRootPart
weld.Part1 = Torso
weld.Parent = Character

RunService.Heartbeat:Connect( function()
	
	local ReferenceCFrame = CFrame.new(HumanoidRootPart.Position, Mouse.Hit.p)
	
	weld.C0 = WeldOffset * HumanoidRootPart.CFrame:toObjectSpace(ReferenceCFrame)
end)

edit: that’s the entire script

sorry this is beyond my scope… I’m not math; I’m hacky. For the above; you would have to convert a vector to angles…

A hacky way to set Part to Camera angle:

A = Camera.CFrame
B = Part.Position

Part.Cframe = A – Camera’s angle
Part.Position = B – Part’s Position

But, I think you can do that in one line, with one of these operations…

CFrame Math Operations (roblox.com)

Note: I never use “Sciptable” because then you gotta continually move the camera by yourself…

For this I think that I liked where, “Attach” placed the Camera, but how “Track” followed the bird…

	mouse.Button1Down:connect(function()
	local chr=sp.Parent
	if chr and lasttrigger.Value+cooldown<tick() then
			local t=chr:FindFirstChild("Head")	
			
		local h=chr:FindFirstChild("Humanoid")
		if t and h and h.Health>0 then
			if check then
					check=false
					remoteEvent:FireServer(t, mouse.Hit.p)
					
					lasttrigger.Value=tick()
					mouse.Icon="rbxasset://textures\\GunWaitCursor.png"
					
					sp.Handle.Transparency=1
					SaveType = workspace.CurrentCamera.CameraType
					SaveSubject = workspace.CurrentCamera.CameraSubject 
					SaveFOV = workspace.CurrentCamera.FieldOfView
					SaveCFrame = workspace.CurrentCamera.CFrame
					
					workspace.CurrentCamera.CameraType = "Attach"
					workspace.CurrentCamera.CameraSubject = workspace.Crow.Eyes
					workspace.CurrentCamera.FieldOfView = 100
					wait(.1)
					workspace.CurrentCamera.CameraType = "Track"

			else
				
				check = true
				workspace.CurrentCamera.CameraType = SaveType
					workspace.CurrentCamera.CameraSubject = SaveSubject
					workspace.CurrentCamera.FieldOfView = SaveFOV
					workspace.CurrentCamera.CFrame = SaveCFrame
			end	
		end
	end
end)

Crow - Roblox

CFrame Operations.

Set the CFrame of the part you want to orient to … = CFrame.new(part.Position, part.Position + currentCamera.CFrame.LookVector)

1 Like

I tried what you said a time ago but i don’t know why but this makes the character slide through baseplate when i move the camera, forgot to put the solution i found here:

local WeldOffset = HumanoidRootPart.CFrame:Inverse() * Torso.CFrame

local Weld = Instance.new("Weld")

Weld.Part0 = HumanoidRootPart
Weld.Part1 = Torso
Weld.Parent = Character

local Attachment = Instance.new("Attachment")
Attachment.Parent = game.Workspace.Terrain

RunService.Heartbeat:Connect( function()

	local ReferenceCFrame = CFrame.new(HumanoidRootPart.Position, Attachment.WorldPosition)

	Weld.C0 = WeldOffset * HumanoidRootPart.CFrame:toObjectSpace(ReferenceCFrame)

	Attachment.Position = Mouse.Hit.LookVector * 10000
end)

I got this solution in another topics i made, i’ll put it here if someone find this one.

1 Like