본문 바로가기
Unity

[GetKey] Input Keys

by kldaji 2021. 7. 12.

1. GetKey

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ${className} : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            transform.Rotate(0, 1, 0);         
        }
        if (Input.GetKey(KeyCode.RightArrow))
        {
            transform.Rotate(0, -1, 0);
        }
    }
}

※ Key 입력을 한 번만 받기 위해서는 GetKeyDown method를 사용하면 된다.

 

2. GetAxis

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ${className} : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
        transform.Rotate(0, Input.GetAxis("Horizontal"), 0);
    }
}

 

 

'Unity' 카테고리의 다른 글

[Animator] 애니메이션  (0) 2021.07.14
[CharacterController] 캐릭터 이동  (0) 2021.07.13
물체 충돌  (0) 2021.07.13
[GetButton] Jump  (0) 2021.07.12
[GetAxis] Moving Object  (0) 2021.07.12