Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Spawning something in front of NPC - how to get NPC direction?

I have an NPC/player that's constantly moving and I want to spawn something (a brick) in front of their current location. By in front, I mean the direction that the NPC/player is facing.

Yes, I get the position of the player and do a +10 or whatever distance I want to put it in front of the NPC. But how can I tell whether to add the +10 on the X or the Z axis of the vector3/cframe? How do I know the direction that the NPC is facing?

2 answers

✓ Accepted answer

Hi Ribasu,

You get the CFrame of the NPC and set it equal to the CFrame of the NPC multiplied by a -10 on the Z axis. Z axis is depth, so relatively, that means in and out from the NPC's relative position. CFrame gets the rotation and position matrix, so it already accounts for the NPC's rotation. So, all you need to do is take its CFrame and multiply it by a negative number on the Z axis. At least, that's how I've done it in the past. Here, I'll show you an example:

brick.CFrame = torso.CFrame * CFrame.new(0, 0, -10);

##Well, I hope I helped and have a nice day.

Thanks,

Best regards,

~~ KingLoneCat

2 comments

0

I have to try it out; but looks promising. Thank you for such a response!

0

No problem. Glad to help.

You can use lookVector property of CFrame

local npc = workspace.NPC
local torso = npc.Torso

local part = Instance.new"Part"
part.Parent = workspace
part.CFrame = torso.CFrame + torso.CFrame.lookVector * 2

2 comments

0

Thanks, this is very educational. but could you please explain what the "* 2" in line 6 is for?

0

To place it a little further away

This discussion is preserved as it was. New answers, comments, and votes are closed.