#Struggling with Movement Logic

4 messages · Page 1 of 1 (latest)

zealous scaffold
#

Hello! I'm trying to make Yume Nikki style movement where everything works on a grid system. When you let go of up for example, it will keep moving you up until you move to a proper position on the grid.
https://youtu.be/Yr4iJL1LZwY?si=o_M7D7ozYrZG9ja_&t=6277

I have figured out how to get it working for up, but it won't work if I just copy it to the other directions because they would start to conflict with each other. How could I make this logic work properly? Is there a simpler way to do this?

extends CharacterBody2D

@export var speed_stat: int = 16
var speed_real: int = speed_stat * 16
var tile_size: float = 16
var moving: bool = false

func _physics_process(delta: float) -> void:
    velocity = Vector2()
    
    if Input.is_action_pressed("up"):
        if moving == false:
            velocity.y -= speed_stat
        velocity.x = 0
    if position.y as int % 16 != 7:
        velocity.y -= speed_stat
        moving = true
    else:
        moving = false
    
    if Input.is_action_pressed("down"):
        position.y += speed_stat
        velocity.x = 0
    #if position.y as int % 16 != 8:
    #    velocity.y += speed_stat
    if Input.is_action_pressed("left"):
        position.x -= speed_stat
        velocity.y = 0
    if position.x as int % 16 != 7:
        velocity.x -= speed_stat
    if Input.is_action_pressed("right"):
        position.x += speed_stat
        velocity.y = 0
    #if position.x as int % 16 != 8:
    #        velocity.x += speed_stat

    move_and_slide()

Game: https://store.steampowered.com/app/650700/Yume_Nikki/

This is a longplay of the Steam version of Yume Nikki, the 2004 cult classic RPG Maker surreal horror adventure from the reclusive Kikiyama. I originally uploaded a version of video on 26 Jan 2019, but I have since realized I accidentally removed the segment in which Madotsuki attains ...

▶ Play video
fringe rune
# zealous scaffold Hello! I'm trying to make Yume Nikki style movement where everything works on a ...

Probably a few ways to do this. The first way I'd give it a go would be to have your character always walking towards a target point (Unless it's within some small distance of that target). Now make it so the target can only be in the center of a grid tile, and once you choose a direction, lock out all movement controls until the character reaches that target. This will result in somewhat tedious movement controls while switching directions, but you can smooth it out by queueing the next movement input when the character is within say, a quarter tile distance of the target.

zealous scaffold