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

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

Asked by
Ribasu 127
6 years ago

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

Log in to vote
0
Answered by 6 years ago

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

0
I have to try it out; but looks promising. Thank you for such a response! Ribasu 127 — 6y
0
No problem. Glad to help. KingLoneCat 2642 — 6y
Ad
Log in to vote
0
Answered by
Amiaa16 3227 Moderation Voter Community Moderator
6 years ago

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
0
Thanks, this is very educational. but could you please explain what the "* 2" in line 6 is for? Ribasu 127 — 6y
0
To place it a little further away Amiaa16 3227 — 6y

Answer this question