Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Best method to detect if a player can pick something up? [closed]

Asked by 6 years ago

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?

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. chomboghai 2044 — 6y
0
Yeah I knew about DistanceFromCharacter, but I'm trying to have this polished and make sure its in front of the player heinekaN 0 — 6y

Closed as Primarily Opinion-Based by User#19524

This question has been closed because it is a discussion about a topic focused on diverse opinions, which isn't a good fit for our Q&A format.

Why was this question closed?