#Right way to adjust AnimatableBody2D's position

1 messages · Page 1 of 1 (latest)

quaint cypress
#

How do I set the position of an AnimatableBody2D, and how do I move it with pushing everything else?

I'm trying to make a simple game which a wall with gap is keep coming from right side, and the player can move up and down to go through the gap. I've used AnimatableBody2D for the gap, and trying to set the random starting position y and I can't find a way

#

obstacle.gd

extends AnimatableBody2D

const SPEED = 100

func _process(delta: float) -> void:
    position.x -= SPEED * delta
#

obstacle_spawner.gd

extends Node

@export var item : PackedScene

func _on_timer_timeout() -> void:
    var newItem = item.instantiate()
    add_child(newItem)
    newItem.global_position.y += randi_range(-50, 50)
quaint cypress
quaint cypress
green timber
#

I don't know much about gdscript, but you should use physics_process for anything that should collide with other objects, like the wall. I don't know why you're having the teleporting issue. Maybe your timer node has incorrect timing and goes too fast? It could also be that the script that restarts the timer isn't waiting for the timer to end before it restarts again?

misty nimbus
#

Use the Remote view to check existing nodes during runtime

quaint cypress
quaint cypress
misty nimbus
#

What is the current problem? is it not moving?

quaint cypress
#

it is moving, but the initial y position is the same

#

so, newItem.global_position.y += randi_range(-50, 50) this one is the problem

#
print(newItem.global_position.y)
newItem.global_position.y += randi_range(-50, 50)
print(newItem.global_position.y)
#

after trying this, I can see it's printing "0" for both print call

#

it's the same if I use position instead of global_position

misty nimbus
#

Maybe it is because you're adding integers to the position? altho it should not be an issue.

There's randf_range()

Still, this is pretty weird.

#

Try type casting the newItem to a Node2D at least.

quaint cypress
#

didn't work...

quaint cypress
misty nimbus
#

yeah.

#

Altho it SHOULD be working already. I do this very often and i'v never had issues with it.

quaint cypress
#

yeah I don't see any problem with the logic

misty nimbus
#

Maybe the fact that it is an AnimatableBody is messing with it? Which wouldn't make much sense, but it is the only difference from my usual approach of using CharacterBody

quaint cypress
#

it's Godot 4.3 and a fresh Forward+ project...

misty nimbus
#

Random question. But, do you have Debug>Show Collision Shapes enabled?
And does your Sprite2D have any non-default settings? Like top_level

#

The property section in the Inspector will say "x changed" if it has non-default values in said section.

quaint cypress
#

I enabled, and it has only default settings

#

because I didn't configure anything

#
  1. new project creation
  2. wall scene creation with AnimatableBody2D, and adding Sprite2D & CollisionShape2D
  3. added Godot's default icon.svg for my Sprite2D
  4. adjusting CollisionShape2D's size (Rectangle selected)
#
  1. added script to AnimatableBody2D and copy-pasted my previous code, except that I've put them in _physics_process() now
  2. main scene creation with Node2D, Camera2D
  3. Spawner node added as Node2D, added Timer
  4. added script to Spawner node and copy-pasted my previous code, in Inspector added the wall scene for newITem
#

that's all I did. maybe changing the Node's display name but it's the only thing

#

@misty nimbus and, yes it was something about AnimatableBody2D as expected

#

CharacterBody2D works well

#

maybe I have to use _integral_forces thing even for the initial position...?