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?
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
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:
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)