입력
입력 행동에는 Down, Up, Stay 세 가지가 있다.
1. Down (누른 순간)
변화: 누르지 않은 상태 -> 누른 상태
2. Up (누르고 떼는 순간)
변화: 누른 상태 -> 누르지 않은 상태
3. Stay (누르고 있는 상태 유지)
< 키보드의 입력 코드 예시 >
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript1 : MonoBehaviour
{
void Update()
{
// 아무 키를 통해 입력 (키보드 or 마우스)
if (Input.anyKeyDown)
Debug.Log("플레이어가 아무 키를 눌렀습니다.");
if (Input.anyKey)
Debug.Log("플레이어가 아무 키를 누르고 있습니다.");
// 키보드를 버튼 입력
// GetKey: 키보드 버튼을 입력받으면 true
if (Input.GetKeyDown(KeyCode.Escape)) // ESC 입력
Debug.Log("설정 창을 출력합니다.");
if (Input.GetKeyDown(KeyCode.Return)) // Enter 입력
Debug.Log("아이템을 구입하였습니다.");
if (Input.GetKey(KeyCode.LeftArrow)) // <- 방향키 입력 유지
Debug.Log("왼쪽으로 이동 중");
if (Input.GetKeyUp(KeyCode.RightArrow)) // -> 방향키 입력 중지
Debug.Log("오른쪽 이동을 멈추었습니다.");
}
}
< 마우스의 입력 코드 예시 >
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript1 : MonoBehaviour
{
void Update()
{
// 아무 키를 통해 입력 (키보드 or 마우스)
if (Input.anyKeyDown)
Debug.Log("플레이어가 아무 키를 눌렀습니다.");
if (Input.anyKey)
Debug.Log("플레이어가 아무 키를 누르고 있습니다.");
// 마우스를 통해 입력
// GetMouseButton 마우스를 통해 입력 받으면 true
if (Input.GetMouseButtonDown(0)) // 마우스 왼쪽 버튼 클릭
Debug.Log("미사일 발사");
if (Input.GetMouseButton(1)) // 마우스 오른쪽 버튼 클릭 유지
Debug.Log("미사일 모으는 중..");
if (Input.GetMouseButtonUp(1)) // 마우스 오른쪽 버튼 떼기
Debug.Log("슈퍼 미사일 발사");
}
}
Edit -> Project Setting -> Input Manager 에서 입력 받을 키와 코드를 연결할 수 있다.
- Size값을 변경하여 버튼을 추가, 삭제할 수 있다.
- 내용을 변경하면 수정할 수 있다.
Name에 해당하는 값을 GetButton()에 인자로 전달해야한다.
< Input Manager에 설정된 값으로 입력 받는 코드 예시 >
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript1 : MonoBehaviour
{
void Update()
{
// 아무 키를 통해 입력 (키보드 or 마우스)
if (Input.anyKeyDown)
Debug.Log("플레이어가 아무 키를 눌렀습니다.");
if (Input.anyKey)
Debug.Log("플레이어가 아무 키를 누르고 있습니다.");
// Edit -> Project Setting -> Input Manager를 통해 저장된 값으로 입력 받기
if (Input.GetButtonDown("Jump")) // 스페이스 버튼 입력 (일반적으로 쓰지 않고 GetButton("Jump")만 사용)
Debug.Log("점프");
if (Input.GetButton("Jump")) // 스페이스 버튼 입력 유지
Debug.Log("점프 모으는 중..");
if (Input.GetButtonUp("Jump")) // 스페이스 버튼 입력 떼기
Debug.Log("슈퍼 점프");
}
}
< 수평 방향의 이동 코드: GetAxis 사용 >
- 왼쪽: -1에 가까운 값
- 오른쪽: 1에 가까운 값
=> 가중치를 통해 이동 거리, 속도를 나타낼 수 있다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript1 : MonoBehaviour
{
void Update()
{
// GetAxis()는 -1 ~ 1을 누른 상태를 유지하는 시간에 따라 반환한다.
if (Input.GetButton("Horizontal"))
{
Debug.Log("횡 이동 중.." + Input.GetAxis("Horizontal"));
}
}
}
< 수평 방향의 이동 코드: GetAxisRaw 사용 >
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript1 : MonoBehaviour
{
void Update()
{
// GetAxisRaw()는 -1, 0, 1을 입력의 여부만 반환한다. (0은 동시 입력)
if (Input.GetButton("Horizontal"))
{
Debug.Log("횡 이동 중.." + Input.GetAxisRaw("Horizontal"));
}
}
}
오브젝트 이동
오브젝트의 Transform
오브젝트 형태에 대한 기본 컴포넌트로 위치, 회전, 크기를 표현한다.
오브젝트은 Transform을 항상 갖고 있어야하는 서로 일대일 대응 관계이다.
=> 한 오브젝트를 생성했을 때, 따로 인스턴스를 생성할 필요없이 transform이라는 변수를 갖고 있다.
Translate(x, y, z)
- 인자로 벡터 값을 받고 그만큼 더해서 이동시키는 함수
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript1 : MonoBehaviour
{
void Update()
{
// 키보드 방향키로 입력을 받아 x축 y축을 이동시킨다.
Vector3 vec = new Vector3(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"), 0);
transform.Translate(vec);
}
}
'Study > Unity Study' 카테고리의 다른 글
[Unity] 게임 오브젝트의 흐름 (0) | 2023.11.09 |
---|