Need help with player moving the background with it

Godot Version

4.2.1

Question

I need help with a player Area2D that is moving things that it shouldn’t.

For the images i attached:
sos: The main scene the game will run in
nova: The player scene
red / blue: testing assets

Intended functionality is for blue to stay in place while red follows the player but these are reversed. How can I fix this?
test2

Player move script:

extends Area2D

signal hit

@export var awareness = 100
@export var speed = 200
@export var damage = 1
@export var attack = 1
@export var gold = 0

func _process(delta):
var velocity = Vector2.ZERO # The player’s movement vector.
if Input.is_action_pressed(“move_right”):
velocity.x += 1
if Input.is_action_pressed(“move_left”):
velocity.x -= 1
if Input.is_action_pressed(“move_down”):
velocity.y += 1
if Input.is_action_pressed(“move_up”):
velocity.y -= 1

if velocity.length() > 0:
	velocity = velocity.normalized() * speed
	$AnimatedSprite2D.play()
else:
	$AnimatedSprite2D.stop()

position -= velocity * delta

if velocity.x < 0:
	$AnimatedSprite2D.animation = "walk_left"
	$AnimatedSprite2D.flip_h = false
elif velocity.x > 0:
	$AnimatedSprite2D.animation = "walk_left" # right
	$AnimatedSprite2D.flip_h = true
elif velocity.y < 0:
	$AnimatedSprite2D.animation = "walk_up"
elif velocity.y > 0:
	$AnimatedSprite2D.animation = "walk_down"
	
print("Nova:" + str(position))

im not quite understand your code, how a player that is Area2D type node has velocity?

I just started Godot so I’m not too sure myself, but this code is taken from Godot’s 2D Tutorial if that helps

oh ok now i see, didnt see that it created its own velocity, used it just for direction for the animated sprite to play correct “animation” based of the velocity

then the following code:

position += velocity * delta
position = position.clamp(Vector2.ZERO, screen_size)

actually make it moves

your code here doesnt look the same like it shown in there

here what i tested, and it should work:
GIF:
blueredmove

what my codes look like:

extends Area2D

@export var speed = 400 # How fast the player will move (pixels/sec).
var screen_size # Size of the game window.

func _ready():
	screen_size = get_viewport_rect().size

func _process(delta):
	var velocity = Vector2.ZERO # The player's movement vector.
	if Input.is_action_pressed("ui_right"):
		velocity.x += 1
	if Input.is_action_pressed("ui_left"):
		velocity.x -= 1
	if Input.is_action_pressed("ui_down"):
		velocity.y += 1
	if Input.is_action_pressed("ui_up"):
		velocity.y -= 1

	if velocity.length() > 0:
		velocity = velocity.normalized() * speed
		$AnimatedSprite2D.play()
	else:
		$AnimatedSprite2D.stop()
	position += velocity * delta
	position = position.clamp(Vector2.ZERO, screen_size)

Tried that and blue is still following the player while red is stationary

Anything else I can provide to help?

Are you sure that you set the blue texture to the blue sprite node and the red texture to the red sprite node?

Yup, would it help if I gave you the file to the project so you can take a look yourself?

what’s inside your nova’s scene?
click this
image

and show what you see on the Local hierarchy

image
Heres what that looks like

Managed to figure out it was an issue of the camera being moved, not the player sprite

Fixed by changing this line:

$AnimatedSprite2D.position += velocity * delta

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.