Trying to make the player bounce left.

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

Hello,

I am trying to make my player bounce to the right side on a “jumppad” and i have already set it up on the Y axis using

func _on_BounceArea_body_entered(body):
velocity.y = -spring_power

I am also trying to do this on the X axis but i cannot get it to work while using the same code, just rewritten for the X axis.

Here’s the code

extends KinematicBody2D

class_name Player

const UP = Vector2(0,-1)

export var gravity = 20 export var speed = 150 export var jump_power =
300 export var spring_power = 500 export var springp_hori = 500

var velocity = Vector2()

var jump_max = 2 var jump_count = 0

func _physics_process(delta): velocity.y += gravity if
Input.is_action_pressed(“move_right”): velocity.x = speed elif
Input.is_action_pressed(“move_left”): velocity.x =- speed else:
velocity.x = 0 if is_on_floor() and jump_count!=0: jump_count =
0 if jump_count<jump_max: if
Input.is_action_just_pressed(“jump”): velocity.y = -jump_power
jump_count += 1 velocity = move_and_slide(velocity, UP) func
_process(delta: float) → void: change_animation()

func change_animation(): if velocity.x > 0: $AnimatedSprite.flip_h
= false elif velocity.x < 0: $AnimatedSprite.flip_h = true if velocity.y < 0: $AnimatedSprite.play(“jump”) else: if velocity.x
!= 0: $AnimatedSprite.play(“walk”) else:
$AnimatedSprite.play(“idle”)

func _on_Fallzone_body_entered(body):
get_tree().change_scene(“res://World.tscn”)

func _on_BounceArea_body_entered(body): velocity.y = -spring_power

func _on_HorizontalBounceArea_body_entered(body): velocity.x =
spring_power

Let me know

:bust_in_silhouette: Reply From: Hexadotz

Are you trying to make a jump pad as if a trampoline that will lunch you in the air if you step on it?
If so you should give it it’s own scene and script then write something like this

func _on_jumppad_body_entered(body):
     If body.is_in_group("player"):
          Body.Velocity.y += bounce_height

However if you’re trying to make it so the player is like a spring you will need to check which direction the player is going to something like this:

func bouncer():
     If input.is_action_pressed("move_right") and Input.is_action_just_pressed("jump"):
          Velocity.y = jump_height
          Velocity.x += bounce_push

This is what i can provide because i don’t really understand what’s exactly you’re trying to achieve, hopefully this will help you