0 votes

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.

Godot version 3.2.3
in Engine by (230 points)
edited by

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.

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.

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

1 Answer

0 votes

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 = (playerposition.x - bossposition.x) / thetimeonair

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

by (341 points)

never mind wrong comment

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.