I am very new to coding so please excuse if this is inefficient, but what I want is to make the camera follow the head of my snake so it is like first person but the code I wrote (I commented which bit is the problem) is definitely wrong and makes the snake camera really trippy, does anyone have any ideas on what I could do?
-- This is the code I am having a problem with, the camera goes weird and trippy
for i = 1,#snake do
--This is the part that is wrong
if i == size then
game.ReplicatedStorage.SnakePart.Position = snake[i]
game.ReplicatedStorage.SnakePart:Clone().Parent = game.Workspace.SnakeBody
workspace.CurrentCamera.Focus = workspace.SnakeBody.SnakePart.CFrame
workspace.CurrentCamera.CFrame = workspace.SnakeBody.SnakePart.CFrame
end
game.ReplicatedStorage.SnakePart.Position = snake[i]
game.ReplicatedStorage.SnakePart:Clone().Parent = game.Workspace.SnakeBody
end
--This is the full code
math.randomseed(tick())
workspace.CurrentCamera.CameraType = 'Scriptable'
--Allows you to change the direction of the snake and keeps you in the same direction if no key is pressed.
game:GetService('UserInputService').InputBegan:connect(function(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.W then
if p_dir~='S'then
direction='N'
end
elseif inputObject.KeyCode == Enum.KeyCode.S then
if p_dir~='N'then
direction='S'
end
elseif inputObject.KeyCode == Enum.KeyCode.A then
if p_dir~='E'then
direction='W'
end
elseif inputObject.KeyCode == Enum.KeyCode.D then
if p_dir~='W'then
direction='E'
end
end
end)
snake={}
x,z = 0,0
size,Wait = 3, 0
isAlive = true
apple = false
direction = 'S'
pDir = direction
while isAlive == true do
workspace['SnakeBody']:Destroy()
myFolder = Instance.new("Folder", workspace)
myFolder.Name = "SnakeBody"
p_dir = direction
if direction=='N' then
z=z-30
elseif direction=='S' then
z=z+30
elseif direction=='W' then
x=x-30
elseif direction=='E' then
x=x+30
end
--Detects if the snake has hit a border and moves it to the opposite side.
if x == 600 and direction == 'E' then
x=0
elseif x == -30 and direction == 'W' then
x=570
elseif z == 600 and direction == 'S' then
z=0
elseif z == -30 and direction == 'N' then
z=570
end
--Adds in the apple and draws it if the apple has been eaten or the game just loaded.
if apple == false then
appleX=(math.random(19)*30)
appleZ=(math.random(19)*30)
game.ReplicatedStorage.Apple.Position = Vector3.new(appleX-15,24,appleZ-15)
game.ReplicatedStorage.Apple:Clone().Parent = game.Workspace
apple=true
end
if #snake==size then
table.remove(snake,1)
end
local vector = Vector3.new(x-15, 24, z-15)
table.insert(snake, vector)
--Checks if the snake ate the apple
if x == appleX and z == appleZ then
size = size+1
workspace['Apple']:Destroy()
apple=false
end
for i = 1,#snake do
--This is the part that is wrong
if i == size then
game.ReplicatedStorage.SnakePart.Position = snake[i]
game.ReplicatedStorage.SnakePart:Clone().Parent = game.Workspace.SnakeBody
workspace.CurrentCamera.Focus = workspace.SnakeBody.SnakePart.CFrame
workspace.CurrentCamera.CFrame = workspace.SnakeBody.SnakePart.CFrame
end
game.ReplicatedStorage.SnakePart.Position = snake[i]
game.ReplicatedStorage.SnakePart:Clone().Parent = game.Workspace.SnakeBody
end
--print(x,z)
--print(unpack(snake))
wait(0.1)
end