#Tween lagging issue

9 messages · Page 1 of 1 (latest)

cyan bronze
#

Hello! I'm coming from Unity and learning Godot 4.
I'm having an issue with tween. Basically it feels like tween is lagging a little bit, here's a video.
You can see on the sprite that it feels like it's lagging... Also, it does the same thing with the animated background because it's a simple Sprite2D attached to the camera and rotating to achieve this effect.

#

Here's my player script:

extends Node2D

@onready var tile_map = $"../TileMap"
@onready var sprite_2d = $Sprite2D
@onready var ray_cast_2d = $RayCast2D

var is_moving = false

func _process(delta):
    if is_moving:
        return
    
    if Input.is_action_pressed("up"):
        move(Vector2.UP)
    elif Input.is_action_pressed("down"):
        move(Vector2.DOWN)
    elif Input.is_action_pressed("left"):
        move(Vector2.LEFT)
    elif Input.is_action_pressed("right"):
        move(Vector2.RIGHT)

func move(direction: Vector2):
    # Get curent tile Vector2i
    var current_tile: Vector2i = tile_map.local_to_map(global_position)

    # Get target tile Vector2i
    var target_tile: Vector2i = Vector2i(
        current_tile.x + direction.x,
        current_tile.y + direction.y,
    )
    
    # Get custom data layer from the target tile
    var tile_data: TileData = tile_map.get_cell_tile_data(0, target_tile)
    
    # If the next tile is not walkable, return
    if tile_data.get_custom_data("walkable") == false:
        return
    
    # Update the raycast to point to the direction
    ray_cast_2d.target_position = direction * 16
    ray_cast_2d.force_raycast_update()
    
    # If there is something in front of us, return
    if ray_cast_2d.is_colliding():
        var colliding_object = ray_cast_2d.get_collider()
        var colliding_parent = colliding_object.get_parent()
        if colliding_parent is Crate:
            colliding_parent.push(direction)
        return
    
    # Move player
    is_moving = true
    
    # Update the player position to the next tile
    global_position = tile_map.map_to_local(target_tile)
    
    # Keep the sprite position at the current tile
    sprite_2d.global_position = tile_map.map_to_local(current_tile)
    
    # Animate sprite using tween
    var tween = create_tween()
    tween.tween_property(sprite_2d, "global_position", global_position, 0.31)
    await tween.finished

    is_moving = false
#

And here's how my tree looks :

#

I've tried tweaking the tween value and changing the type, but no luck... Still that weird lag effect

#

I wonder if I made a mistake somewhere

delicate ether
#

i think the perfect solution is to drop the tween in those cases and do the movement in _process, making sure you handle the pixel coordinates nicely (prefer undershooting and risk just taking a frame longer for the last movement, is less noticeable than overshooting and stepping back)

according to that code you're still using a tween though.

cyan bronze
#

ooh I understand now

#

I'm scared that the performance would be very bad doing that

#

that just doesn't seem "right" in my head