Moving the character

Hi all,
I am trying to move a character when the “right” key is pressed but it seems I am not doing it the correct way.

This is my attempt:

local direction = 0

-- ...........

function update(self, dt)
    local pos = go.get_position()
    local new_position = pos.x + direction
    go.set_position(vmath.vector3(new_position,0,0))
end

function on_input(self, action_id, action)
    if action_id == hash("right") then
    	print("Right key pressed")
    	direction = 1
    end
end

I can see in the console that the debug message “Right key pressed” is printed but my character does not move. I suspect the error is related to the way I try to update the game object’s position. My logic is the following: get current position, increment the x coordinate by 1, and then set this new position.

Would appreciate your help.

The only reason I can think of why it’s not moving right is because you’ve not attached the script to the game object (go).
Also with this logic, you will only need to press the key once, and it will continue to move right.

:thinking: Have you set a “right” event in your input bindings?

As mentioned in the first post, when I press the right key I can see the log message in console, so this proves the script is attached to the game object and the KEY_RIGHT event is set.

I suspect the issue lies in the way I try to set the new position, maybe I am not using correctly the vector3?
Because I put a log statement in the update() method and I can see that indeed the “new_position” is not changed, it is always 87.989089892

OK, must have been half asleep when I answered, place this in your script

function init(self)
    -- acquire input focus as soon as the instance has been initialized
    msg.post(".", "acquire_input_focus")
end

You have to point the input to the script, also a good tip is to multiply it by dt, to allow for various frame rates on different devices, but you will also need to increase the move amount.

Ok,

KEY_RIGHT event checked…

Then have you tried using a greater value of the X velocity? instead of 1 use 10 or 100 and see what happens.

Some thoughts:

  1. You probably want to use self.direction and in init() set it to 0. If you use local direction the variable will be shared between all instances of the script.
  2. You should also check action.pressed in your on_input() function: if action_id == hash("right") and action.pressed then
  3. I suggest that you also use the dt value when moving the character to ensure that the character moves the correct number of pixels per frame (if the framerate for some reason varies between devices or over time). Maybe something like: local new_position = pos.x + (10 * direction * dt) where 10 is speed in pixels per second.

Otherwise I don’t see any problems with the snippet of code you posted.

3 Likes

OK, the issue was that my character had the collision property to “Dynamic”. This was preventing the ability to control the character position with the keyboard.

I have changed the type to “Kinimatic” and now I can control the character, but now there’s another problem: previously when the scene was created the character was falling down till the ground was hit. Also, it was possible to make the character “jump” by applying a force on the collisionobject. But now, after changing to “Kinematic”, the character simply stays in the air, the laws of physics are no longer applied on it.

My question is how can I have the character both take in consideration the physics, but also to be able to control it with the keyboard?

You will either have to implement your own physics if you go for kinematic collision objects (ie applying gravity as in the getting started tutorial with the frog) or move your character by applying forces to it if you use dynamic objects.

You could possibly also disable the dynamic collision object (using msg.post("#collisionobject", “disable”) when you need to move the game object through go.set_position() and then re-enable it again.

Thank you for you answer, really helped me.