Godot Version
4.4.1
Question
Hello, I’m new to Godot so maybe this is very stupid, but I searched a lot on Google and couldn’t find anything.
I wanted to create an object (let’s say a zombie) that moves towards a static object like a house in a constant speed. I used an AnimatableBody2D for this.
My Code for the zombie looks like this:
@onready var house: StaticBody2D = $"../House"
const SPEED = 50
func _process(delta: float) -> void:
position += (house.position - position).normalized() * delta * SPEED
However, this animation is veeeery slow and also like 80% of the times you start the game, it moves in a very stuttering and flickeringway, but sometimes it also moves more clean.
But when I change the type of the zombie to CharacterBody2D, it moves a lot faster and is not flickering anymore. Why does the code behave so differently depending on the node type? Shouldn’t there be the exact same results, because I’m directly setting the position of the zombie?
Positions are composed; global_position
is the position of a thing in the world, while position
is where it is relative to its parent. Were both nodes at the same place in the tree?
Does the animation for your zombie have any position changes in it?
Depends on a lot of factors, but general you shouldn’t use an AnimatableBody2D for a character. AnimatableBody2D is better suited to world collision that moves consistently, like a moving platform that sways side to side.
If you are using the exact same code then it should behave about the same, the physics engine may get in the way of directly changing a physics body’s position, as physics are supposed to take forces like velocity.
If you are using the same calculation for velocity
and move_and_slide()
then it will be about 1/60th the speed, because delta
is already factored in during move_and_slide()
so you would be doubling the per-second reduction in speed.
1 Like
Your problem is likely because you put your code in _process() instead of _physics_process(). If you don’t want jumpy display issues, use _physics_process() - it is designed to deal with framerate issues. _process() is not.
- If this is like Plants vs/ Zombies, the house is always there, but you may move it in your tree. Just make it easy to find by right-clicking on your house node and selecting Access as a unique name. (However if it’s always in the same place you don’t really need it referenced here at all.)
- Don’t make your speed a constant. It’s useless if you ever want a different zombies move at different speeds. Instead make it an export variable.
- Like @gertkeno said, don’t use delta in your velocity calculation.
- CharacterBody2D is the node intended for what you are doing. You can use move_and_slide() to handle the actual movement. You can do it manually the way you are trying to, but you’re going to run into more issues.
This code should work for you:
@export var speed: int = 50
func _physics_process(_delta: float) -> void:
velocity.x = -speed #Assuming PvZ style where zombies are always moving left.
move_and_slide()
1 Like