Alright, so lately i’ve decided to make a game that’s a direct copy of Celeste (a platformer that revolves around dashing and some other stuff).
If you’ve ever played it, you know that theres 8 directions that you can dash in, those Up, Right, Left, Down and the diagonals.
However… Bringing this to 3D is pretty tough, as you get a new dimension that you have to do trigonometry in (i’ve got zero experience in this).
I understand the baaaasics of trigonometry (as i go to school) and know that cos is the X axis in a 2d space, and sin is the Y axis in a 2d space. Anyway, i’ve made a rather simple script that… doesn’t work.
When i add up the math.abs of the X, Y and Z vectors they a lot of times result in a value that’s bigger than 1, which should not happen, it should ALWAYS equal 1 as i want my dashes to be consistent.
Here’s the script!! Please, help me with this and explain how you fixed my script, as i think i will have to use trigonometry a LOT.
local pressingS = keybinder.AskForKeybind("S") -- backward
local pressingW = keybinder.AskForKeybind("W") -- forward
local pressingR = keybinder.AskForKeybind("R") -- upper
local pressingQ = keybinder.AskForKeybind("Q") -- downer
local pressingD = keybinder.AskForKeybind("D")
local pressingA = keybinder.AskForKeybind("A")
local camCF:CFrame = cameraasking:Invoke()
local _, y, _ = camCF:ToEulerAnglesYXZ()
local yangle = math.deg(y)
local right = not checkifnil(pressingD)
local left = not checkifnil(pressingA)
local forward = not checkifnil(pressingW)
local backward = not checkifnil(pressingS)
local upward = not checkifnil(pressingR)
local downward = not checkifnil(pressingQ)
local negativeYOffset = (backward and not forward) and true or false
local Ynumber = 0
local Xnumber = 0
-- getting the x offset
local Xangletable = {}
if forward then table.insert(Xangletable, 0) end
if backward and not forward then table.insert(Xangletable, -180) end
if upward then table.insert(Xangletable, 90) end
if downward then table.insert(Xangletable, -90) end
if #Xangletable > 0 then
print(Xangletable)
for i, v in Xangletable do
Xnumber += v
end
Xnumber /= #Xangletable
end
local Yangletable = {}
if forward or backward then table.insert(Yangletable, 0) end
if left then table.insert(Yangletable, 90) end
if right then table.insert(Yangletable, -90) end
if #Yangletable > 0 then
print(Yangletable)
for i, v in Yangletable do
Ynumber += v
end
Ynumber /= #Yangletable
if negativeYOffset then Ynumber = -Ynumber end
print(Ynumber)
end
yangle += Ynumber
local Yvector = math.sin(Xnumber)
local Xvector = math.cos(Xnumber) * math.sin(yangle)
local Zvector = math.cos(Xnumber) * math.cos(yangle)
print(math.abs(Yvector)+math.abs(Xvector)+math.abs(Zvector)) --prints a value that's NOT 1 (bad)
some clarifications: the xangletable and the yangletable i use to get the arithmetic mean of the player’s inputs. the angle offsets work decent… i think? im not sure if my problems are because my angles go over 180, that may actually be the case. if it is, please tell me how to fix it as i’ve got no idea.