I'm trying to make a system where players can pick objects up. But I want to make sure the object is in front of their character. Right now I use CFrame to determine if its in front of the player:

local Displacement = Character.HumanoidRootPart.CFrame:pointToObjectSpace(Object.Position)

if math.abs(Displacement.X) < AngleReach and Displacement.Z > -Distance and Displacement.Z < 0 then
	--print("The object is in front of the character. They may pick it up.")
else
	--print("The object is behind the character. They can not pick it up.")
end

But are there better more efficient ways to do this? Right now the code above runs for every object on the ground. Would I be better off with using Raycasting or even have a part that is welded to the front of the character and test if its touching an object?

What would you guys recommend?

2 comments

0

Does the object have to be in front? There's a function http://robloxdev.com/api-reference/function/Player/DistanceFromCharacter which gives you the distance between an object and a character's head, so you could use that and check if the object is within a certain distance. This isn't ideal if the player needs to be facing the object though.

0

Yeah I knew about DistanceFromCharacter, but I'm trying to have this polished and make sure its in front of the player