(이 강의는 골드메탈 님의
"키보드 마우스로 이동시켜보자"를 보고 정리한 내용입니다.)
키보드에는 총 3가지 행동지표가 있다.
엔터 = Return 이라고한다! 유니티에서!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class p1 : MonoBehaviour
{
void Update()
{
if(Input.anyKeyDown)
Debug.Log("플레이어가 아무 키를 눌렀습니다.");
if(Input.GetKeyDwon(KeyCode.Return))
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 p1 : MonoBehaviour
{
void Update()
{
if(Input.anyKeyDown)
Debug.Log("플레이어가 아무 키를 눌렀습니다.");
if(Input.GetMouseButtonDown(0))
Debug.Log("미사일 발사");
if(Input.GetMouseButton(0))
Debug.Log("미사일 모우는중");
if(Input.GetMouseButtonUp(0))
Debug.Log("슈퍼 미사일 발사!!!!");
}
}
숫자를 인자로 받는데 0 은 왼쪽버튼 1은 오른쪽 버튼이다.
Edit -> Project Settings -> Input Manager
에 가면 유니티제공 인풋 Button 들을 확인할수 있다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class p1 : MonoBehaviour
{
void Update()
{
if(Input.anyKeyDown)
Debug.Log("플레이어가 아무 키를 눌렀습니다.");
if(Input.GetButtonDown("Jump"))
Debug.Log("점프 !");
if(Input.GetButton("Jump"))
Debug.Log("점프 모으는중");
if(Input.GetButtonUp("Jump"))
Debug.Log("슈퍼 점프! !!!!");
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class p1 : MonoBehaviour
{
void Update()
{
if(Input.anyKeyDown)
Debug.Log("플레이어가 아무 키를 눌렀습니다.");
if(Input.GetButton("Horizontal"))
{
Debug.Log("횡 이동 중 . . ."+Input.GetAxis("Horizontal"));
}
}
}
GetAxixRaw -> -1,+1,0 딱 떨어지게 값을 반환해준다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class p1 : MonoBehaviour
{
void Update()
{
if(Input.anyKeyDown)
Debug.Log("플레이어가 아무 키를 눌렀습니다.");
if(Input.GetButton("Horizontal"))
{
Debug.Log("횡 이동 중 . . ."+Input.GetAxisRaw("Horizontal"));
}
if(Input.GetButton("Vertical"))
{
Debug.Log("종 이동 중 . . ."+Input.GetAxisRaw("Vertical"));
}
}
}
Vector3 = 3D
Vector2 = 2D
(방향x)스칼라 값 -> 벡터(방향 ㅇ)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class p1 : MonoBehaviour
{
void Start()
{
}
void Update()
{
Vector3 vec = new Vector3(0,0.1f,0); // 벡터 값
transform.Translate(vec);
}
}
이렇게하면 매순간 업데이트 되면서 벡터값이 상승하므로 쭈우욲 올라간다 !
그런데..
카메라가 못따라잡는다!!
그렇다면 카메라도 상승하면!?
카메라에 P1을 넣어주면
같이 상승한다!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class p1 : MonoBehaviour
{
void Start()
{
}
void Update()
{
Vector3 vec = new Vector3(Input.GetAxis("Horizontal"),Input.GetAxis("Vertical"),0); // 벡터 값
transform.Translate(vec);
}
}
이렇게 인풋값으로 받으면 플레이어가 누른 키에 따라 움직이게 된다!