Weird 8 Direction Movement? (SOLVED)

So I’ve been trying to make some 8 directional, and it works fine, except it seems like it only detects one key at a time.

I can press up to go up, but if I press down while pressing up it doesn’t stop and it continues to go up. I can press right to go right, but if I press up while pressing right it will just go up and not go diagonal.

Why am I getting this problem?

Here is my movement code, if it helps:

local speed = 8;

local input_x = { left = hash("move_left"), right = hash("move_right") };
local input_y = { up = hash("move_up"), down = hash("move_down") };

function init(self)
	self.direction = vmath.vector3(0, 0, 0);
	
	msg.post(".", "acquire_input_focus");
end

function update(self, dt)
	local old_position = go.get_position(".");
	local movement = vmath.vector3(speed * self.direction.x, speed * self.direction.y, 0);

	go.set_position(old_position + movement, ".");
end

function on_input(self, action_id, action)
	local new_direction = vmath.vector3(0, 0, 0);

	if (action_id == input_x.left) then
		new_direction.x = new_direction.x + -action.value;
	end
	if (action_id == input_x.right) then
		new_direction.x = new_direction.x + action.value;
	end
	
	if (action_id == input_y.down) then
		new_direction.y = new_direction.y + -action.value;
	end
	if (action_id == input_y.up) then
		new_direction.y = new_direction.y + action.value;
	end
	
	self.direction = new_direction;
end

Hi, I’m still quite new to Defold so I’m not sure if this is right but I think it is because the on_input function only reads one input at a time. There is only one action_id so it can’t be both up and left for example.

What should work is if you update the self.direction x and y directly in the on_input function instead of creating a local new_direction variable and set both the x and y of self.direction back to 0 and the end of the update function.

In the War battles tutorial on the Defold site they do it like that.

Hope this helps

3 Likes

Seems to work great now. Thanks for the help.

2 Likes