Untitled
unknown
plain_text
a year ago
1.9 kB
1
Indexable
Never
extends KinematicBody2D var velocity = Vector2.ZERO var speed = 200 var direction = Vector2(1, -1).normalized() const gravity = 500 var collision_type = "" func _physics_process(delta): # Zastosowanie prędkości dla kierunku ruchu var motion = velocity * delta # Sprawdzenie, czy obiekt dotyka ziemi var collision = move_and_collide(motion, false, false, true) if is_on_floor(): print("floor") if collision: # Obliczenie wektora normalnego w punkcie kolizji var normal = collision.normal # Obliczenie kierunku odbicia direction = direction.bounce(normal) # Określenie kąta między kierunkiem odbicia a wektorem (-1, 0) var angle = rad2deg(direction.angle()) # Określenie najbliższego kierunku odbicia if normal.y == -1: collision_type = "floor" else: collision_type = "wall" print(collision_type) if collision_type == "floor": if angle > 0 and angle < 90: direction = Vector2(1, 1) elif angle >= 90 and angle < 180: direction = Vector2(-1, 1) elif angle <= -90 and angle > -180: direction = Vector2(-1, -1) elif angle <= 0 and angle > -90: direction = Vector2(1, -1) velocity.y = -velocity.y -10 if collision_type == "wall": # print(angle) if angle > 0 and angle < 90: direction = Vector2(1, 1) elif angle >= 90 and angle < 180: direction = Vector2(-1, 1) print(str(direction) + "prawa") elif angle <= -90 and angle > -180: direction = Vector2(-1, 1) elif angle <= 0 and angle > -90: direction = Vector2(1, 1) print(str(direction) + "lewa") velocity.y = -velocity.y # Resetowanie prędkości w osi y po odbiciu # Zastosowanie grawitacji velocity.y += gravity * delta # Zastosowanie kierunku i prędkości dla ruchu velocity.x = direction.x * speed velocity = move_and_slide(velocity, Vector2.UP)