Godot Version
v4.3.stable.steam
Question
Hello everyone! I have been looking for a solution to this problem for a long time.
And so it all started with the fact that I decided to add a box to my game that the player can push. But as soon as I did this, a huge problem appeared! If you jump on the box or push it against the wall, it will start to twitch very strongly and eventually it will be thrown in a random direction or it will go through the floor!
I found this video, and the author describes exactly the same problem. And his solution really helped me! But there is one thing. Now my box after a couple of pixels catches the corner of the floor soliziya and my character kind of winds up on the box and flies over it
Let me say right away that the problem is not in the collision of TileMap tiles!
Should I use this solution?! And why does it work so strangely for me? Please help!
wchc
January 19, 2025, 10:56pm
2
Please share your code using preformatted text with ```
1 Like
extends CharacterBody2D
var speed: int = 300
var jump_speed : int = -400
var gravity : int = 2500
@export_range(0.0, 1.0) var friction = 0.6
@export_range(0.0 , 1.0) var acceleration = 0.4
var push_forse = 30.0
var jump_veloscity : int = 0 #определяет высоту прыжка
var jump_timer : bool = true
func _ready() -> void:
get_tree().paused = false
$".".position = $"../Spavn".position
func _physics_process(delta: float) -> void:
run()
jump(delta)
platform()
animations()
chek_box()
move_and_slide()
func run():
var dir = Input.get_axis("left", "right")
var dir_gamepad = Input.get_axis("left_gamepad", "right_gamepad")
if dir != 0:
velocity.x = lerp(velocity.x, dir * speed, acceleration)
elif dir_gamepad != 0:
velocity.x = lerp(velocity.x, round(dir_gamepad) * speed, acceleration)
else:
velocity.x = lerp(velocity.x, 0.0, friction)
if is_on_floor() or is_on_ceiling():
friction = 0.7
acceleration = 0.35
else:
friction = 0.02
acceleration = 0.2
func jump(delta):
if Global.graviti_unver == false and !is_on_floor() and jump_timer:
jump_timer = false
$JumpTimer.start()
elif Global.graviti_unver == true and !is_on_ceiling() and jump_timer:
jump_timer = false
$JumpTimer.start()
if (Input.is_action_just_pressed("up") or Input.is_action_just_pressed("up_gamepad")) and (!is_on_floor() or !is_on_ceiling()) and $JumpTimer.is_stopped():
jump_veloscity = 15
if Input.is_action_pressed("up")or Input.is_action_pressed("up_gamepad"):
jump_veloscity += 100 * delta
elif Input.is_action_just_released("up") or Input.is_action_just_released("up_gamepad"):
jump_veloscity = 15
if Global.graviti_unver == false and is_on_floor() and !is_on_ceiling():
jump_veloscity = 0
jump_timer = true
elif Global.graviti_unver == true and is_on_ceiling() and !is_on_floor():
jump_veloscity = 0
jump_timer = true
if (Input.is_action_pressed("up") or Input.is_action_pressed("up_gamepad")) and jump_veloscity < 15 :
velocity.y = jump_speed
else:
velocity.y += gravity * delta
func platform():
if (Input.is_action_pressed("down") or Input.is_action_pressed("down_gamepad")):
set_collision_mask_value(3,false)
else:
set_collision_mask_value(3,true)
func animations():
if velocity.x != 0 and (is_on_floor() or is_on_ceiling()):
$AnimatedSprite2D.play("run")
elif velocity.y >= 0 and (!is_on_floor() and !is_on_ceiling()):
$AnimatedSprite2D.play("jump_down")
elif velocity.y < 0 and (!is_on_floor() and !is_on_ceiling()):
$AnimatedSprite2D.play("jump_up")
else:
$AnimatedSprite2D.play("idl")
if velocity.x > 0:
$AnimatedSprite2D.flip_h = false
elif velocity.x < 0:
$AnimatedSprite2D.flip_h = true
func chek_box():
for i in get_slide_collision_count():
var c = get_slide_collision(i)
if c.get_collider() is RigidBody2D:
c.get_collider().apply_central_impulse(-c.get_normal() * push_forse)