I’m trying to figure out how to script a system where an object’s sprite changes based on the player’s viewing angle, similar to how enemies worked in classic games like DOOM or Wolfenstein 3D
What I mean is:
When the camera moves around an object, its sprite updates to show a different angle (like front, side, back views, etc). This gives the illusion of 3D while actually just using 2D images
Set up 4 Points (1 for each side, excluding top and bottom… unless you wanna include it), and get the closest Point to the camera, this will tell you which side is the closest to the camera, if it’s the front side/point, then show the front view sprite, and so on
Similar to the first one, but use ToObjectSpace(), which gives you the offset or difference between 2 CFrames… like for example, Entity.CFrame:ToObjectSpace(Camera.CFrame) will give the CFrame Offset between the Entity and the Camera
Do you have example pictures or a vid of what you are talking about? I am trying to figure out what you mean, camera moves around an object… is the players camera not always top down and centered in the center?
You can do this by using the Dot Product. In my case, I have a simple enemy here that I’ll use as an example. By comparing the camera direction to the enemy’s LookVector and RightVector, we can tell whether we’re viewing the front, back, left, or right side of the enemy. A positive dot with the LookVector means we’re viewing it from the front, and a negative one means we’re viewing it from the back. Same logic applies to the RightVector.
local RunService = game:GetService("RunService")
local enemy = workspace:WaitForChild("Enemy")
local label = enemy.BillboardGui.TextLabel
local camera = workspace.CurrentCamera
RunService.RenderStepped:Connect(function()
local direction = (camera.CFrame.Position - enemy.Position).Unit
local front = direction:Dot(enemy.CFrame.LookVector)
local right = direction:Dot(enemy.CFrame.RightVector)
if front >= 0.5 then
label.Text = "Front"
elseif front < -0.5 then
label.Text = "Back"
elseif right >= 0 then
label.Text = "Right"
else
label.Text = "Left"
end
end)
The reason why we’re checking whether the front dot is higher or lower than 0.5 is so that we can support diagonal viewing. If it was just 0, we wouldn’t be able to tell if we’re looking from the right or left side.
Given this logic, you can probably handle the switching of sprites yourself.