Hi i tried making a boss that dashes to you and when it hits a wall it becomes dizzy, but i want it to dash only on left and right, never up or down, but since i did player.position - position, it will do any direction, either up, down, left, or right (dont mind that my boss is a dorito, its just a test) and im not used to using state machines (the direction variable is a Vector2 variable) and my boss is following the player like it has no gravity, i want it to have gravity and dash, my direction = player.position - position
Definitely has an issue ( i added if not is_on_floor():
Velocity += get_gravity() * delta, that code works 100%) dont mind my main character, my pixel art skills arent that good
Okay, i think it only moves left and right now which is good, but, if i jump on it and be slightly to the right, the boss would go to the right very slowly, probably because its mixing up and right or something, so i want the speed nice and consistent, i dont want it to change, and its still flying while following
Did you normalize the direction? The boss moving very slowly is what I referred to when I said ânormalize the direction to keep the attack from looking silly in case the player is only a smidgen to the right or left of the bossâ.
To make the boss obey gravity; in your boss script, you probably have a line of code that looks something like this:
velocity = direction * speed * delta
Itâs probably somewhere in _process or _physics_process. That would be a good place to handle gravity. Example:
velocity = direction * speed * delta
if not is_on_floor: velocity.y += some_gravity_constant
edit: it looks like youâre doing this in _physics_process:
func _physics_process(delta: float) -> void:
velocity = direction
You generally want the direction vector to have a magnitude of 1 (or 0, in case the boss is standing still), which is what the normalization is for. Normalization takes a vector and ensures it has a magnitude of 1. Then you can multiply it by a scalar (speed) to set the bossâs velocity.
_on_area_2d_body_entered changes the direction. So after setting the y-component of the direction to 0 would be a good place to normalize the direction.
Are there other places in your code where you change the direction? If so, normalize there as well. (Also a friendly tip, donât share code via screenshots but post it as preformatted text. i.e. by enclosing it with three backticks (```). This makes reading and understanding your code much easier.)
direction has been declared as a variable, but has not been instantiated yet. Its value is null, yet is accessed in _process and _physics_process. Try instantiating it like this instead:
@onready var player = get_parent().find_child("player")
@onready var animatedsprite = $AnimatedSprite2D
var direction : Vector2 = Vector2(0, 0)
Judging by the video, it is falling. Just not very fast. What happens if you change velocity += get_gravity() * delta in:
func _process(delta: float) -> void:
#direction = player.position - position
# Add the gravity.
if not is_on_floor():
velocity += get_gravity() * delta
with velocity += 10000 * delta? (Change 10000 to some slightly smaller big number if that causes problems. For instance, if the boss phases through the floor.)