This is the testing Godot forums! All forum posts unique to this forum will be deleted! Please use the main forums here for any posts you want to keep. All forum rules still apply.

Trying to get a smooth grid based movement

NamnamNamnam Posts: 2Member
edited March 2019 in Programming

Hi everybody,
Juste started to explore godot yesterday (i come from unity) and i'm trying to get a smooth four direction grid based movement kind like rpg maker does but when my camera follow my character the movement sutter a little bit.
So i' got a :

TileMap Grid Ysort Player Tween Sprite Camera2D Background Sprite

One thing that i find strange is that if my camera is the last child in player, the player will always be well centered with the camera but if it's not the last child, the player will shake a little bit. Camera have no smooth or margin (always make it worst)

Here is the code i attached to my player

extends Node2D


var grid
var direction = Vector2()
export(float) var speed =5


func _ready():
    grid = get_parent().get_parent()
    speed = 1.0/speed


func _physics_process(delta):
    direction = _get_four_directions()
    if direction!=Vector2():
        _move_player()


func _move_player():
    set_physics_process(false)
    var target = position + grid.get_cell_size()*direction
    $Tween.interpolate_property(self, "position", position, target, speed, Tween.TRANS_LINEAR, Tween.EASE_IN)
    $Tween.start()
    yield($Tween, "tween_completed")
    set_physics_process(true)


func _get_four_directions():
    if Input.is_action_pressed("ui_right"):
        return Vector2(1,0)
    if Input.is_action_pressed("ui_left"):
        return Vector2(-1,0)
    if Input.is_action_pressed("ui_up"):
        return Vector2(0,-1)
    if Input.is_action_pressed("ui_down"):
        return Vector2(0,1)
    else:
        return Vector2()

So do you have a idea why my movement seem to sutter ? Is it something from my code or something from my camera ?


Tags :

Answers

  • NamnamNamnam Posts: 2Member

    I use the console to see if my character stopped at some point by printing the difference between his position and his position at the previous frame. I stayed pressed to the left but it randomly give zero point a bunch of numbers, always just after the end of a move to a cell. Most of the time it works great but it still does that (often after between 3 and 9 moves completed).
    Still i don't understand why it does that (or why it doesn't does that every time).

  • ZoeyZanidooZoeyZanidoo Posts: 35Member

    Juste started to explore godot yesterday (i come from unity) and i'm trying to get a smooth four direction grid based movement kind like rpg maker does but when my camera follow my character the movement sutter a little bit.

    I had a little issue like that, and it presented itself because I was moving one tile at a time, but if the player is finishing its tween to the new location but is still pressing the button, it stops and waits a step before registering that the player wants to continue, creating a little stutter. I fixed this by checking for the end of the tween After it does a tween step that turn, and if input is still held down and a move in the specified direction is valid, I let the player take its next turn now instead of next time _Process is called.

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.