How to detect which way the player is facing

So it is kind of hard to describe what my problem is, but I will do my best.

I have a build system where you can build using blocks, and when they have their mouse aimed into the void it will let them “bridge” easily.

Well I have a problem where when you go into first person, and bridge, you can only bridge in the forward direction, because I am using CFrame. How can I make it so you can go all directions?

Here is my code that determines which way to build:

local CF = FloorPart.CFrame*CFrame.new(Vector3.new(0,0,-3))

If you need the whole code, let me know, but you shouldn’t really need it all. I am planning to go far with this game and perfer not to leak the code for the reason of exploiters.

Use the LookVector of the player’s CFrame. As the name suggests, it’s the vector which the part is “looking” at.

The LookVector is a unit vector, meaning that it has a length of 1, so to move it 3 studs forwards you multiply it by 3, or to move it backwards you can multiply by -3 (or subtract instead of adding).

local CF = FloorPart.CFrame + humanoidRootPart.CFrame.LookVector * 3

This allows me to build in ways that are not following the grid pattern.
image

I came up with my own solution by changing the block’s CFrame based on the player’s CFrame, but rounding to the nearest 90 degree to keep it with the grid. Then it will build forward.

HOWEVER:
there is still some things that could be improved, so if you know how to help with these let me know

  1. I raycast down every time to check what the part they are standing on is. Is there an easier, less expensive way to get the part the player is standing on. (expensive meaning not having to draw rays 3 times a second)

  2. You can only build forward by one block, meaning when standing on a block near the void, you can only build forward by one block at a time, making it difficult to bridge fast.

For the second one, you might want this code:

for i,v in pairs(game.Workspace.Map:GetChildren())do
	if c == false then
		if v.Position == CF.Position then
			c = true
		end
	end
end

This checks if there is a part already there, I have tried doing this for a second block, but it doesn’t work:

for i,v in pairs(game.Workspace.Map:GetChildren())do
	if c == false then
		if v.Position == CF*CFrame.new(Vector3.new(0,0,-3).Position then
			c = true
		end
	end
end

Thanks, Jail

Just fixed #2 on my list. Just need a solution to raycasting.

Raycasts are really, really cheap. Doing even thousands of raycasts a second won’t have any noticeable impact on performance so you don’t need to worry about any performance issues.

On that note, a new API might come out sometime which would make the raycasts unnecessary:

2 Likes

Oh, I thought they were super expensive. Thanks!

1 Like