Boss running problems

4.4

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


Something like the enemy in the video

Maybe I misunderstand what you’re going after, but is there a reason you can’t simply only update the boss’s x-direction? Something like:

direction.x = player.position.x - position.x
direction.y = 0
# Add gravity handling on y-direction here.

And maybe then 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.

2 Likes

After computing the direction, you could simply set the y component to zero, so that you only have the right/left on the x axis.

Edit: @TokyoFunkScene was quicker and has an code example :grin:

1 Like

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

When the state switches to dizzy, its gravuty turns back on lol

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.

1 Like

Hi, im not a coding pro, so i have a question, where do i normalize direction? In the “velocity = direction” line or in the direction variable

The direction variable.

Edit: although, either option should be fine.

1 Like

How?

Nvm

_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.)

1 Like

Uh i dont know why it says this error, by the way heres my full code for the boss only, not the states


@onready var player = get_parent().find_child("player")
@onready var animatedsprite = $AnimatedSprite2D

var direction : Vector2
const SPEED = 500.0
const JUMP_VELOCITY = -400.0

func _ready() -> void:
	#direction = player.position - position
	set_physics_process(false)

func _process(delta: float) -> void:
	#direction = player.position - position
	# Add the gravity.
	if not is_on_floor():
		velocity += get_gravity() * delta
		
	if direction.x < 0:
		animatedsprite.flip_h = true
	else:
		animatedsprite.flip_h = false

	move_and_slide()
func _physics_process(delta: float) -> void:
	velocity = direction.normalized() * SPEED * delta
	#move_and_slide()
	#move_and_collide(velocity * delta)


func _on_hitbox_body_entered(body: Node2D) -> void:
	if body.is_in_group("player"):
		player.take_damage(2)


func _on_area_2d_body_entered(body: Node2D) -> void:
	direction.x = player.position.x - position.x
	direction.y = 0



Why are bosses so complex😔

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)
1 Like

Thanks, my error is gone, but… i still have the flying issue

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.)

1 Like

It gives an error
(Thanks for trying to solve this long irritating problem lol)

Sorry, I forgot velocity is a Vector2.

Try:

velocity += Vector2(0, 10000) * delta

(Replace 10,000 with a smaller number if it causes unexpected behavior.)

1 Like

Hey it works!

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