Godot Version
4.3
Question
So, I have been having problems with jittering sprites, I have fixed it thanks to the help from this community. However, now I am trying to implement another aspect of movement, which is pushing/pulling objects. This is a grid based movement game, so with that I decided that you would activate pushing an object, and then be able to move around with it.
I have it so the item will be moved around by the player, however, the sprite doesn’t perfectly follow the player, and there is a lot of sprite jitter. I am wondering if anyone can point me in a better direction to implement my pushing/pulling methods. Here is the code I have atm for implementing a basic push/pull system.
Another unintended consequence is that the push/pull also causes the players sprite to get knocked off the 32 pixel grid slowly, so after a few movements it is a few pixels off the original 32 pixel grid.
extends Area2D
# Objects code that will be being pushed
@export var pushable = true
@export var walkable = true
var being_pushed : bool
var obj_pushing : Node2D
var facing_dir : Vector2
func _physics_process(_delta: float) -> void:
if being_pushed:
global_position = obj_pushing.global_position.round() + facing_dir
func update_push(is_pushed:bool,obj:Node2D) -> void:
being_pushed = is_pushed
obj_pushing = obj
if being_pushed:
facing_dir = _find_facing()
else:
facing_dir = Vector2.ZERO
print(facing_dir)
# Finds where the object is in relation to player
func _find_facing() -> Vector2:
return global_position - obj_pushing.global_position
extends Node2D
# Movement code
enum {NORTH,SOUTH,WEST,EAST} # Matches player direction enums
var move_dist : int = 32 # Clarity for what the number is used for
var move : bool
func _move(player:Node2D,dir:int) -> void: # Controls players movement
var target_pos = player.position
match dir:
NORTH:
target_pos.y -= move_dist
SOUTH:
target_pos.y += move_dist
WEST:
target_pos.x -= move_dist
EAST:
target_pos.x += move_dist
target_pos = target_pos.round()
player.position = player.position.round()
var tween = create_tween()
tween.set_trans(Tween.TRANS_LINEAR) # Use linear interpolation
tween.set_ease(Tween.EASE_IN_OUT)
# Ensure movement happens in physics process
tween.set_process_mode(Tween.TWEEN_PROCESS_PHYSICS)
# Move to target position
tween.tween_property(player, "position", target_pos, player.speed)
# Handle movement completion
tween.tween_callback(func():
if player.state == player.State.MOVE:
player.state = player.State.IDLE
player._motion(dir)
elif player.state == player.State.PUSH_MOVE:
player.state = player.State.PUSH