#2D entities jumping and bouncing of an entity

11 messages · Page 1 of 1 (latest)

storm gyro
#

I have some slimes that go back and forth between a wall horizontally. I want them to jump on the icon when they get near the entity at the middle (the godot icon).
Orange line is what they're currently doing, the red line is what I want them to do.
I can get the distance to the entity, but the math involved in making the slime jump towards/on the entity then bouncing from the button is beyond me.

var velocity = Vector2.ZERO
var speed = 500
var current_speed: int = speed
var right = true
var is_regening = false
var level = 0
var power = 10

var current_experience = 0
var required_experience = 10

var button

func _ready():
    button = $"../Area2D"
    
func _physics_process(delta: float) -> void:
    $AnimatedSprite.flip_h = not right
    velocity = move_and_slide(velocity, Vector2.UP)
    if is_regening: 
        energy.current += energy.per_second * delta
        current_speed = 0
        $AnimatedSprite.play("idle")
    else:
        energy.current -= 1 * delta
        current_speed = speed 
        $AnimatedSprite.play("walk")        
        
    if energy.current < 0:
        is_regening = true
    if energy.current > energy.cap:
        is_regening = false
    
    if is_on_wall():
        right = !right
    
    if right:
        velocity.x = current_speed
        velocity.y = 100
    if not right:
        velocity.x = -current_speed
        velocity.y = 100
        ```
hidden oracle
#

I think you forgot to post the picture 😉

storm gyro
#

It's no longer a slime, but a demon gdyeet

storm gyro
#

Bump! Any help is appreciated.

limpid ether
#

Hi im still a fairly beginner, but I think you can accomplish this by applying - velocity.y when its a certain distance from the entity (or using Raycast2D and detecting if it collide with the entity), then when it on top of the entity, check the collider using get_slide_collision().collider and if the collider is the entity, apply another -velocity.y

#

you may need to adjust the value of velocity.x, velocity.y and the distance to jump from to get the desired result

#

here is what i come up with by editing some old project if it similar to what you trying to achieve

storm gyro
storm gyro
#

It works! 😄