function playerMovement() -- Contains the player movement logic
if love.keyboard.isDown("w") then
Player.y = Player.y - Player.speed * deltaT
end
if love.keyboard.isDown("a") then
Player.x = Player.x - Player.speed * deltaT
end
if love.keyboard.isDown("s") then
Player.y = Player.y + Player.speed * deltaT
end
if love.keyboard.isDown("d") then
Player.x = Player.x + Player.speed * deltaT
end
end
function playerSetup() -- Contains the player and his fields
Player = {} -- Create a table to store the player fields
Player.texture = love.graphics.newImage("assets/player.png") -- Import the texture of player and places it in a variable
Player.x = love.graphics.getWidth()/2-- Player X location is equal to the width of the windows divided by 2
Player.y = love.graphics.getHeight()/2 -- The same from above, but is the Player Y location
Player.size = 0.45 -- Player size (Scale factor)
Player.speed = 250 -- Player speed
Player.r = 0 -- Player radians
end
What you have shown should work already; but if the issue is that your player is faster along the diagonals than if you only moved on one axis, then you need a method to check for diagonal movement explicitly, and then divide Player.speed by math.sqrt(2).
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
The code is fine. Just make the speed adjustment in case both X and Y speeds are bigger than 0.
Rewriting:
function playerMovement() -- Contains the player movement logic
if love.keyboard.isDown("w") then
Player.y = Player.y - Player.speed * deltaT
end
if love.keyboard.isDown("a") then
Player.x = Player.x - Player.speed * deltaT
end
if love.keyboard.isDown("s") then
Player.y = Player.y + Player.speed * deltaT
end
if love.keyboard.isDown("d") then
Player.x = Player.x + Player.speed * deltaT
end
Player.x = (Player.y ~= 0) and (Player.x * 0.7071) or (Player.x)
Player.y = (Player.x ~= 0) and (Player.y * 0.7071) or (Player.y)
Duck Duckinson wrote: ↑Thu Jul 26, 2018 2:53 am
[...]
Player.x = (Player.y ~= 0) and (Player.x * 0.7071) or (Player.x)
Player.y = (Player.x ~= 0) and (Player.y * 0.7071) or (Player.y)
[...]
Unless I'm misunderstanding this, in OP's original code Player.x and Player.y represented the position of the player, the code you've written there assumes they're the velocity.
I'd do it a bit like this (code written for ease of understanding; there are several micro-optimisations that could be done if required)
-- (Assuming `deltaT` is a global variable)
function playerMovement()
local dx, dy = 0, 0
if love.keyboard.isDown("w") then dy = -1 end
if love.keyboard.isDown("a") then dx = -1 end
if love.keyboard.isDown("s") then dx = 1 end
if love.keyboard.isDown("d") then dy = 1 end
if dx ~= 0 or dy ~= 0 then
if dx ~= 0 and dy ~= 0 then
dx = dx * 0.7071
dy = dy * 0.7071
end
Player.x = Player.x + dx * Player.speed * deltaT
Player.y = Player.y + dy * Player.speed * deltaT
end
end