728x90
반응형
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
public class PlayerController : MonoBehaviour
{
void Start()
{
}
void Update()
{
// w ->앞
if (Input.GetKey(KeyCode.W))
{
transform.position += new Vector3(0.0f, 0.0f, 1.0f);
}
// s->뒤
if (Input.GetKey(KeyCode.S))
{
transform.position -= new Vector3(0.0f, 0.0f, 1.0f);
}
if (Input.GetKey(KeyCode.A))
{
transform.position -= new Vector3(1.0f, 0.0f, 0.0f);
}
if (Input.GetKey(KeyCode.D))
{
transform.position += new Vector3(1.0f, 0.0f, 0.0f);
}
}
}
|
cs |
Input.GetKey : 유저가 키를 누르는 동안 true를 반환해준다.
앞,뒤로 갈 때는 z축의 값이 변하고
양옆으로 갈 때는 x축의 값이 변하는것을 transform.position을 통해 조절해준다.
https://docs.unity3d.com/kr/530/ScriptReference/Input.html
Unity - 스크립팅 API: Input
Input Manager에 설정된 각 축을 읽고 모바일 장치의 멀티터치/가속도계 데이터에 접근을 하는 경우에 이 클래스를 사용합니다. 다음의 기본 설정 축과 Input.GetAxis 함수를 이용해서 해당 축을 읽습니
docs.unity3d.com
<중요!>
play버튼 누르고 마우스로 화면 한번 클릭해줘야 캐릭터가 움직인다!
728x90
반응형
'Unity > 기초' 카테고리의 다른 글
[Unity 기초] transform과 Transform (0) | 2021.09.27 |
---|---|
[Unity] Singleton 패턴 (0) | 2021.08.16 |
[Unity] Component 패턴/매니저 만들기 (0) | 2021.08.16 |
[Unity] Object 클래스, Component 클래스,Behavior 클래스 기능정리 (0) | 2021.08.16 |
[Unity] 유니티 입문: 윈도우(창) 기능별 정리, 단축키정리 (0) | 2021.06.26 |
댓글