#Problem trying to create smoother snake-like movement

3 messages · Page 1 of 1 (latest)

remote scroll
#

The code is this (Sorry for the mess, it's for a game jam and I don't have time to optimize it):

Whole player scene and head:




export (PackedScene) var Body
var body_parts = 10
onready var head = get_node("Head")
var direction = Vector2.ZERO
var prev_direction = Vector2.ZERO
var speed = 250
var parts = []
var positions = []


func _ready():
    positions.pop_front()
    head.position = Vector2(0, 0)
    for i in body_parts:
        var body = Body.instance()
        body.position = Vector2((0 - (i+1) * 16), 0)
        parts.append(body)
        add_child(body)


func _physics_process(delta):
    
    if direction != Vector2.ZERO:
        if Input.is_action_just_pressed("right"):
            prev_direction = direction
            direction = Vector2(1, 0)
            positions.append(head.global_position)
            Update_bodies()
        
        if Input.is_action_just_pressed("left"):
            prev_direction = direction
            direction = Vector2(-1, 0)
            positions.append(head.global_position)
            Update_bodies()
        
        if Input.is_action_just_pressed("down"):
            prev_direction = direction
            direction = Vector2(0, 1)
            positions.append(head.global_position)
            Update_bodies()
        
        if Input.is_action_just_pressed("up"):
            prev_direction = direction
            direction = Vector2(0, -1)
            positions.append(head.global_position)
            Update_bodies()
    
    head.move_and_collide(direction * speed * delta)
    

func _input(event):
    if direction == Vector2.ZERO:
        if event is InputEventKey:
            direction = Vector2(1, 0)


func Update_bodies():
    for i in parts.size():
        parts[i].Update_movement(head.global_position, direction)
#

Body parts:



var direction = Vector2.ZERO
var change_to_direction = []
var prev_direction = Vector2.ZERO
var speed = 250
var positions = []

var first_time = true


func _physics_process(delta):
    move_and_collide(direction * speed * delta)
    
    
    if first_time and !positions.empty():
        direction = change_to_direction[0]
        positions.pop_front()
        change_to_direction.pop_front()
        first_time = false
    
    
    if !positions.empty():
        
        if direction.x == 1:
            if global_position.x > positions[0].x:
                direction = change_to_direction[0]
                positions.pop_front()
                change_to_direction.pop_front()
        elif direction.x == -1:
            if global_position.x <= positions[0].x:
                direction = change_to_direction[0]
                positions.pop_front()
                change_to_direction.pop_front()
        elif direction.y == 1:
            if global_position.y > positions[0].y:
                direction = change_to_direction[0]
                positions.pop_front()
                change_to_direction.pop_front()
        elif direction.y == -1:
            if global_position.y <= positions[0].y:
                direction = change_to_direction[0]
                positions.pop_front()
                change_to_direction.pop_front()
    



func Update_movement(new_position, new_direction):
    change_to_direction.append(new_direction)
    positions.append(new_position)