Calculate arc targeting player.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By 9BitStrider

I’m making a 2D game in which one of the bosses jumps to a fixed height.

Example:

const GRAVITY = 900
const JUMP_STR = -350

var jump = false
var jump_delay = 60
var velocity = Vector2.ZERO

func _physics_process(delta):
	if is_on_floor():
		jump _delay -= 1

		if jump_delay == 0:
			#velocity.x = (THIS IS WHERE I'M HAVING TROUBLE.) <===
			velocity.y = JUMP_STR
			jump = true
	
	velocity.y += GRAVITY * delta
	
	velocity = move_and_slide(velocity, Vector2(0, -1))

What I’m wanting to do is calculate velocity.x to make the boss attempt to land on the player’s x position which is calculated when the boss jumps. Unfortunately, I haven’t the foggiest idea on how to do this. Please help.

Is this code the player’s code, or the boss’s code? Could the player’s “x” position be passed to the boss’s script when the boss jumps? Maybe a signal could be used to pass the player’s position, e.g. the player node has a signal, boss_jump, that passes the player’s position via a function, _on_boss_jump(), that’s in the boss’s script.

Ertain | 2021-09-07 23:14

This is an example of the boss’s jump code. There’s a signal in place that pulls the player’s x position when his jump delay hits 0 in the actual script.

9BitStrider | 2021-09-07 23:24

Then how about, when the boss leaps, use an interpolation/tween to move the boss into place.

Ertain | 2021-09-08 02:09

:bust_in_silhouette: Reply From: dethland

It is possible to calculate the _velocity.x .

Once the boss wants to jump, it will take the player’s position vector named player_position.

The boss will record its current position as boss_position.

The _vlocity.x = (player_position.x - boss_position.x) / the_time_on_air

The_time_on_air is a fixed value that you can calculate or you can preset it.

never mind wrong comment

dethland | 2021-09-09 18:50