Unity

[GetKey] Input Keys

kldaji 2021. 7. 12. 16:36

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);
    }
}