드디어 내가 원하던 게임을 만들 수 있는 시간이 왔다
SSAFY에서 1학기 Django와 Vue를 배우면서 Python과 JavaScript에 익숙해지고
2학기에 Spring Boot를 사용해보면서 Java에 익숙해졌으니
이제 정말 내가 원하던 남자의 로망 게임을 만들어 볼 준비가 됐다고 생각한다.
Unity는 또 C#을 사용해서 만들지만 C#은 자바와 굉장히 비슷한 문법을 가지고 있기 때문에 전혀 문제가 되지 않을 것 같다.
개발을 너무 늦은 나이에 시작한 것이 아닌가라는 생각도 들지만, 늦게 시작했기 때문에 더 쉽고 빠르게 공부할 수 있다는 건 큰 메리트라고 생각한다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
Vector2 inputVec;
Rigidbody2D rigid;
public float speed;
// Start is called before the first frame update
void Awake()
{
rigid = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
inputVec.x = Input.GetAxisRaw("Horizontal");
inputVec.y = Input.GetAxisRaw("Vertical");
}
private void FixedUpdate()
{
/* // 1. 힘을 준다
rigid.AddForce(inputVec);
// 2. 속도 제어
rigid.velocity = inputVec;
*/
// 3. 위치 이동
Vector2 next = inputVec.normalized * speed * Time.deltaTime;
rigid.MovePosition(rigid.position + next);
}
}
원래는 이런식으로 Player가 이동을 한다.
힘을 주거나, 속도를 제어하거나 혹은 MovePosition으로 위치를 이동시키는 방식으로 이동을 하는데
Input Vector를 만들고 거기에 GetAxis값들로 Input Manager에 있는 Horizontal과 Vertical값을 가져오는 방식이다.
이 과정을 Plyaer Input을 통해 간단하게 처리해줄 수가 있게 됐다.
위와 같이 Player Input 컴포넌트를 생성할 수 있고 액션을 만들어서 간단하게 Input값으로 Vector값을 조절할 수 있게 됐다.
다음과 같이 Press 됐을 때 Vector값을 이동하고 이동되는 값은 normailze로 일반화해준다.
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
Vector2 inputVec;
Rigidbody2D rigid;
public float speed;
// Start is called before the first frame update
void Awake()
{
rigid = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
}
private void FixedUpdate()
{
/* // 1. 힘을 준다
rigid.AddForce(inputVec);
// 2. 속도 제어
rigid.velocity = inputVec;
*/
// 3. 위치 이동
Vector2 next = inputVec * speed * Time.fixedDeltaTime;
rigid.MovePosition(rigid.position + next);
}
void OnMove(InputValue value)
{
inputVec = value.Get<Vector2>();
}
}
그럼 OnMove라는 함수로 InputValue 값을 받아올 수가 있고, value에서 우리가 넣어둔 Processor의 값 Vector2를 가져와서 넣어주면 이 한 줄로 굳이 InputManager를 이용하지 않고 간단하게 이동을 처리할 수 있게 되었다.