Games/Godot Engine

#Chapter.12 - 속도를 사용하여 오브젝트 움직이기

Ermael Starius 2022. 6. 20. 20:11

 

 

< 사용된 Key Word >

 

「 var velocity = Vector2.ZERO 」 - 속도변수를 기본값 0으로 초기화시킴.

 

「 if  ∽  elif  ∽  else: 」 - 유니티의 조건문 If ~ else if ~ else 구문과 같다.

 

「 move_and_collide() 」 - 해당 파라미터의 값만큼 오브젝트를 이동시킨다.

 

 


#현재 노드의 타입을 상속받음.
extends KinematicBody2D


#속도변수 velocity 를 기본값으로 초기화시킴.
var velocity = Vector2.ZERO


#상하좌우 움직일 시, 속도 3으로 움직임. 키입력 없을 시 정지상태.
func _physics_process(delta):
       if Input.is_action_pressed("ui_right"):
              velocity.x = 3
              print("오른쪽으로 속도 3 움직임")
       elif Input.is_action_pressed("ui_left"):
              velocity.x = -3
              print("왼쪽으로 속도 3 움직임 ")
       elif Input.is_action_pressed("ui_up"):
              velocity.y = -3
              print("위쪽으로 속도 3 움직임")
       elif Input.is_action_pressed("ui_down"):
              velocity.y = 3
              print("아래쪽으로 속도 3 움직임")
       else:
              velocity.x = 0
              velocity.y = 0
              print("정지상태~!")

       #이동처리함수. 해당 파라미터의 입력값 만큼 이동시킴.
       move_and_collide(velocity)

 


 

 

결과: 상하좌우로 움직임. (각 방향에 대한 로그 기록 남김.)  /  두 개의 키가 동시에 작동하지 않음.