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

Spawn part in front of player's head depending on camera's rotation?

Asked by 4 years ago
Edited 4 years ago

Hopefully this should be a quick question, just need some help with CFrame math.

I want to spawn a throwable object in front of the player's head, depending on where the player's camera is looking.

Click for an Epic Paint Drawing of what I'm talking about

After Googling I've tried:

local throwOffset = CFrame.new(0,0,-5)
part.CFrame = head.CFrame * throwOffset

but obviously this only works horizontally, because the player's head isn't rotating up and down.

How do I add the player's camera's look rotation onto this?

Thank you in advance!

1 answer

Log in to vote
0
Answered by 4 years ago

Just found a solution!

local throwOffset = 5 --studs in front of head for part to spawn
local newPos = head.Position + camera.CFrame.lookVector*throwOffset
part.CFrame = CFrame.new(newPos,newPos+camera.CFrame.lookVector)
Ad

Answer this question