Unity

유니티 키보드, 마우스로 이동 + 게임 오브젝트 이동

hsooooo 2021. 8. 8. 14:04

 

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

public class LifeCycle : MonoBehaviour
{
    void Update()
    {
        //anyKeyDown - 아무 입력을 최초로 받을 때 true
        if (Input.anyKeyDown)
            Debug.Log("플레이어가 아무 키를 눌렀습니다.");
        if (Input.anyKey)
            Debug.Log("플레이어가 아무 키를 누르고 있습니다.");

        //키보드 입력 : Down, Stay, Up
        //GetKey - 키보드 버튼 입력을 받으면 true
        if (Input.GetKeyDown(KeyCode.Return)) //Return은 Enter, ESC는 Escape
            Debug.Log("아이템을 구입하였습니다.");

        if (Input.GetKey(KeyCode.LeftArrow))
            Debug.Log("왼쪽으로 이동 중.");

        if (Input.GetKeyUp(KeyCode.RightArrow))
            Debug.Log("오른쪽 이동을 멈추었습니다.");

        //마우스 입력
        //GetMouse - 마우스 버튼 입력을 받으면 true
        //숫자 0 - 마우스 왼쪽 버튼, 1 - 마우스 오른쪽 버/
        if (Input.GetMouseButtonDown(0))
            Debug.Log("플레이어가 아무 키를 눌렀습니다.");
        if (Input.GetMouseButton(0))
            Debug.Log("미사일 모으는 중...");
        if (Input.GetMouseButtonUp(0))
            Debug.Log("슈퍼 미사일 발사!!!");

        //버튼 입력
        //Edit - Project Settings - Input Manager에서 종류 볼 수 있음!
        //그리고 Button 새로 추가 및 기존 변경 가능
        //GetButton - Input 버튼 입력을 받으면 true
        if (Input.GetButtonDown("Jump"))
            Debug.Log("점프!");
        if (Input.GetButton("Jump"))
            Debug.Log("점프 모으는 중...");
        if (Input.GetButtonUp("Jump"))
            Debug.Log("슈퍼 점프!!");

        if(Input.GetButton("Horizontal"))
        {
            Debug.Log("횡 이동 중..." + Input.GetAxisRaw("Horizontal"));
            //GetAxis - 수평, 수직 버튼 입력을 받으면 float 
            Input.GetAxis("Horizontal");

            //오브젝트는 변수 transform을 항상 가지고 있음.
        }

        if (Input.GetButton("Vertical"))
        {
            Debug.Log("횡 이동 중..." + Input.GetAxisRaw("Vertical"));
        }

    }
}

 

 

Transform : 오브젝트 형대에 대한 기본 컴포넌트

*오브젝트는 변수 transform을 항상 가지고 있음.

 

 

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

public class LifeCycle : MonoBehaviour
{
    void Start()
    {
        //Translate - 벡터 값을 현재 위치에 더하는 함수
        //Vector3(3차원)
        Vector3 vec = new Vector3(5, 5, 5);
        transform.Translate(vec);

        //int number = 4; // 스칼라(순수한 값)
        //Vector3 vec = new Vector3(0, 0, 0); // 벡터(방향과 그에 대한 크기 값)
    }

    void Update()
    {
        Vector3 vec = new Vector3(
        	Input.GetAxis("Horizontal"),
            Input.GetAxis("Vertical"),
            0);
        transform.Translate(vec);
    }
}