How to Raycast from camera?

Okay, I actually have two questions. I’m working on a picking up and holding system, but I want it to work in first person and third person, so I want to know how I can Raycast from the camera rather than the mouse, so it will work in both. I’m having trouble implementing this. Another question, how would I stop the parts from spazzing like crazy? In the video I attached it spazzed out a bit, I’m not sure how to stop it. If anyone could help me with these problems that would be nice.

And my current Raycast code (If someone can show me how to use WorldRoot:Raycast() that would be cool!)

function RaycastFromMouse(Range)
	local RayIgnoreList = {Player.Character or nil}
	for i,v in pairs(PartIgnoreList:GetDescendants()) do
		if v:IsA("BasePart") or v:IsA("MeshPart") then
			table.insert(RayIgnoreList,v)
		end
	end
	local mouseRay = Mouse.UnitRay
	local newRay = Ray.new(mouseRay.Origin,mouseRay.Direction.Unit * Range)
	local target,pos,norm = workspace:FindPartOnRayWithIgnoreList(newRay,RayIgnoreList)
	return {target,pos,norm}
end

That’s it!

Wait, so what position are you trying to use? Mouse or Camera?

Currently, I am Raycasting from the mouse. I would like to Raycast from the Camera's CFrame instead. Also, how would I stop the part spazzing?

local CAMCF = workspace.CurrentCamera.CFrame

local result = workspace:Raycast(CAMCF.Position, CAMCF. LookVector * 1000, rayparams)

I have no clue as to what causes the spazzing tho.

Is there a way that I can prevent WorldRoot:Raycast() from returning the endpoint of the ray as nil? I would like to be able to find the Vector3 of the endpoint even if the ray does not end on a part.

There are no endpoints. Just put a condition checking if its nil and make it set it to CFrame + LookVector * Radius

At one point, while it was spazzing, I printed out the RotVelocity of the part and it was not Vector3.new(0,0,0). I’m curious as to what causes it to change, as Roblox says it is deprecated which makes me think they would not have physics change that value. None of my scripts change the value either.

Have you checked if it is anchored?

The part is not anchored, I want physics to interact with it. Instead, I added a BodyForce that takes into account the gravity in workspace and the parts mass to cause it not to fall.

Then it is normal the velocity changes. Plus, if you are changing its position every frame, why would you want it not anchored?

Let me see what happens if I anchor them, give me a moment

If that doesnt work, make sure that model is blacklisted on the RaycastParams

I would like it to be unanchored when they are not holding it. How should I get the original value of anchored?

The current system uses WorldRoot:FindPartOnRayWithIgnoreList() I will migrate it over to WorldRoot:Raycast(). Can you give me an example of good RaycastParams for this scenario?

Make it a variable.

You would be better off looking up RaycastParams.new() as I am not home at the moment.

Now they can go through the floor when I’m holding them. The part is anchored now. Do you know why this is? It does not do it when the part is unanchored.

Can you show the code as it is?

Not sure how to fix this :confused:

I can try other stuff…

I was still reading the code, but since you editted I couldnt continue. First thing I noticed is you dont need the task.wait on the end of the event. Secondly, are you changing each part relatively? You would be better off making it a model and then having the player only move the PrimaryPart while the other parts would be welded to this one.

That is what I did for the crate model, and I added the crate model into a folder in workspace that is the ignore list for the ray. Then I welded one part in the folder HoldParts to the crate model.

Sorry I didn’t mean to take away the code lol