2021-08-21 Godot Engine 견과&우유 사다리 등 실시

15256 단어 Godot
작업 환경
Windows 10 Pro v21H1
Godot Engine v3.3.2.stable.official
참고 자료
  • Best way to detect collisions
  • https://godotforums.org/discussion/25828/best-way-to-detect-collisions
  • KinematicBody2D 사용
  • https://docs.godotengine.org/ja/stable/tutorials/physics/using_kinematic_body_2d.html
  • 개요
  • 점프 가능한 지역형 설치
  • 중력으로 낙하
  • 견과류와 우유 같은 사다리
  • 사다리와 접촉할 때 닫힌 중력 낙하
  • 장면
  • Node2D
  • Area2D: 사다리
  • CollisionShap2D
  • AnimatedSprite: 젓가락 이미지
  • KinemuticBody2D: 유저
  • AnimatedSprite
  • CollisionShap2D
  • TileMap: 바닥
  • 문자 작업
  • 왼쪽, 오른쪽 커서: 가로 이동
  • 스페이스 바: 점프
  • 이루어지다
    유저에 첨부된 스크립트
    Player.gd
    extends KinematicBody2D
    
    export (int) var run_speed = 200
    export (int) var gravity = 1000
    export (int) var jump_speed = -400
    
    var velocity = Vector2()
    var jumping = false
    var area_entered = false  # はしご接触中
    
    onready var area = get_node("../Area2D")  # はしご
    
    func _ready():
        pass # Replace with function body.
    
    func get_input():
        velocity.x = 0
        var right = Input.is_action_pressed("ui_right")
        var left = Input.is_action_pressed("ui_left")
        var jump = Input.is_action_just_pressed("ui_accept")
    
        # はしごに接触しているか検知
        var isOverlap = false
        for body in area.get_overlapping_bodies():  # Area2DにPlayerが入ったことを検知
            isOverlap = true
        if isOverlap:
            area_entered = true
        else:
            area_entered = false
    
        # その他の処理
        if jump and is_on_floor():
            jumping = true
            velocity.y = jump_speed 
        if right:
            velocity.x += run_speed
        if left:
            velocity.x -= run_speed
    
    func _physics_process(delta):
        get_input() 
        if area_entered:  # はしご接触中
            velocity.y = 0.0
        else:
            velocity.y += gravity * delta
        if jumping and is_on_floor():
            jumping = false
        velocity = move_and_slide(velocity, Vector2(0, -1))
    
    실행 결과
    점프 후 사다리에 오르는 상태

    시험을 준비하다
    견과류와 우유도 사다리를 오르내리며 움직였지만 이번에는 실장하지 않았다.처리가 복잡하지 않다.
    사다리를 오르내리다
    상하 이동이 이루어졌다.잘 설치되지 않았습니다.
    Player.gd
    extends KinematicBody2D
    
    export (int) var run_speed = 200
    export (int) var gravity = 1000
    export (int) var jump_speed = -400
    
    enum {
        YDIR_FALL = 0,
        YDIR_STOP,
        YDIR_UP,
        YDIR_DOWN,
    }
    var velocity = Vector2()
    var jumping = false
    var area_entered = false  # はしご接触中
    var ydir_type = YDIR_FALL
    
    onready var area = get_node("../Area2D")  # はしご
    
    
    func _ready():
        pass # Replace with function body.
    
    func get_input():
        velocity.x = 0
        var right = Input.is_action_pressed("ui_right")
        var left  = Input.is_action_pressed("ui_left")
        var up    = Input.is_action_pressed("ui_up")
        var down  = Input.is_action_pressed("ui_down")
        var jump  = Input.is_action_just_pressed("ui_accept")
    
        # はしごに接触しているか検知
        var isOverlap = false
        for body in area.get_overlapping_bodies():  # Area2DにPlayerが入ったことを検知
            isOverlap = true
        if isOverlap:
            area_entered = true
        else:
            area_entered = false
    
        # その他の処理
        if jump and is_on_floor():
            jumping = true
            velocity.y = jump_speed 
        if right:
            velocity.x += run_speed
        if left:
            velocity.x -= run_speed
        if area_entered:
            ydir_type = YDIR_STOP
            if up:
                ydir_type = YDIR_UP
            if down:
                ydir_type = YDIR_DOWN
        else:
            ydir_type = YDIR_FALL   
    
    func _physics_process(delta):
        get_input() 
        if ydir_type == YDIR_STOP:  # はしご接触中
            velocity.y = 0.0
        elif ydir_type == YDIR_UP:
            velocity.y -= run_speed * delta
        elif ydir_type == YDIR_DOWN:
            velocity.y += run_speed * delta
        else:
            velocity.y += gravity * delta
        if jumping and is_on_floor():
            jumping = false
        velocity = move_and_slide(velocity, Vector2(0, -1))
    

    좋은 웹페이지 즐겨찾기