Reputation: 91
I have a question that I've been wanting to know the answer to for a while. How to get the position and direction the player character is looking? I want to know this because I need to make a system that involves this.
Upvotes: 1
Views: 2850
Reputation: 26
So in ROBLOX, all BaseParts
have a property named CFrame
which represents the Position and Orientation of that BasePart
.
Now if you wanna find the where the character is looking, we could check the direction the character's Head
is facing by utilizing its CFrame
. (since we can't get the CFrame
of a model). To do this, we can reference the character Head
and then get the LookVector
property of its CFrame
. And voila you got the direction where the character is facing. But there is a slight issue, you see-LookVector
is not a positional vector but rather a directional vector, as such it will have a Magnitude
(length) of 1. So if we want to find the position the character is looking at, we need to multiply this LookVector
by a number which will denote the number of studs in the direction of the character we want to look and then add it with the position of the Head
.
So based on the above, you can do this:
local Head: BasePart = LocalPlayer.Character.Head.CFrame
local Direction: Vector3 = Head.LookVector
local Distance: number = 3 -- Look 3 studs in the direction of the `Head`
local Target: Vector3 = Head.Position + (Direction * Distance)
Upvotes: 0