Created
November 5, 2024 00:43
-
-
Save lamarmarshall/3a5279e4810da73838102d8ef7320973 to your computer and use it in GitHub Desktop.
defold, lua, character, movement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| local MOVE_SPEED = 100 | |
| function init(self) | |
| -- Add initialization code here | |
| -- Learn more: https://defold.com/manuals/script/ | |
| -- Remove this function if not needed | |
| msg.post("@render:", "use_camera_projection") | |
| msg.post(".", "acquire_input_focus") | |
| self.velocity = vmath.vector3() | |
| end | |
| function update(self, dt) | |
| -- Add update code here | |
| -- Learn more: https://defold.com/manuals/script/ | |
| -- Remove this function if not needed | |
| end | |
| function fixed_update(self, dt) | |
| local pos = go.get_position() | |
| pos = pos + self.velocity * MOVE_SPEED * dt | |
| go.set_position(pos) | |
| self.velocity = vmath.vector3() | |
| end | |
| function on_message(self, message_id, message, sender) | |
| -- Add message-handling code here | |
| -- Learn more: https://defold.com/manuals/message-passing/ | |
| -- Remove this function if not needed | |
| end | |
| function on_input(self, action_id, action) | |
| if action_id == hash("move_up") then | |
| print("move up") | |
| self.velocity.y = 1 | |
| elseif action_id == hash("move_down") then | |
| self.velocity.y = -1 | |
| end | |
| if action_id == hash("move_left") then | |
| self.velocity.x = -1 | |
| elseif action_id == hash("move_right") then | |
| self.velocity.x = 1 | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment