Godot Version
4.3
Question
Hello! Can someone explain this to me? When I move the box to the wall, it jumps above my character. Here are some scripts for the player and the box. The box is also very bouncy, no matter how much I change its mass or gravity.
player.gd
extends CharacterBody2D
const SPEED: float = 425.0
const JUMP_VELOCITY: float = -370.0
@onready var animated_sprite: AnimatedSprite2D = %AnimatedSprite2D
func _physics_process(delta: float) -> void:
if not is_on_floor(): # Гравитация
velocity.y += get_gravity().y * delta
if Input.is_action_just_pressed("JUMP") and is_on_floor(): # Прыжок
velocity.y = JUMP_VELOCITY
var direction: float = Input.get_axis("MOVE_LEFT", "MOVE_RIGHT") # Направление движения
if direction > 0:
animated_sprite.flip_h = false
if direction < 0:
animated_sprite.flip_h = true
if is_on_floor():
if direction == 0:
animated_sprite.play("IDLE")
else:
animated_sprite.play("RUN")
if direction != 0:
velocity.x = direction * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
move_and_slide()
for index: int in range(get_slide_collision_count()):
var collision := get_slide_collision(index)
var collider := collision.get_collider()
if collider is RigidBody2D:
var box: RigidBody2D = collider
var push_power := 2.0
var box_collision := box.move_and_collide(Vector2(velocity.x * delta, 0))
if box_collision:
var box_collider := box_collision.get_collider()
# Исправленная проверка группы с приведением типа
if box_collider is Node:
var node_collider := box_collider as Node
if node_collider.is_in_group("wall"):
continue
var push_force := Vector2(
clamp(velocity.x, -SPEED, SPEED) * delta * push_power as float,
0
)
if abs(velocity.x) > 10:
box.apply_central_force(push_force)
box.linear_velocity.y = clamp(box.linear_velocity.y, -50 ,50)
Box.gd
extends RigidBody2D
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
freeze_mode = RigidBody2D.FREEZE_MODE_KINEMATIC # Опционально
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass