[유니티 공부] 2D 슈팅게임 만들기 #1 오브젝트 이동 구현(키보드/마우스)

반응형

유니티 왕초보입니다.

열심히 취미로 공부중이고 자주 사용하는 코드를

기록하면서 공부시작!

 

1. 오브젝트(캐릭터) 이동 구현

public float speed = 3f;

private void Update()
{
float inputX = Input.GetAxisRaw("Horizontal"); float inputY = Input.GetAxisRaw("Vertical");

transform.Translate(new Vector2(inputX, inputY) * Time.deltaTime * speed);
}

1. speed 변환 값 주기

2. 일단 먼저 키보드로 구현

3. 모바일기반 마우스버튼으로 구현 예정

 

2. 콜라이더 부딛힘 떨림 조정

public float speed = 3f;

public bool isTop;
public bool isBottom;
public bool isRight;
public bool isLeft;

private void Update()
{
float x = Input.GetAxisRaw("Horizontal");
if ((isRight && x == 1) || (isLeft && x == -1))
x = 0;
float y = Input.GetAxisRaw("Vertical");
if ((isTop && y == 1) || (isTouchBottom && y == -1))
y = 0;

Vector3 curPos = transform.position;
Vector3 nexPos = new Vector3(x, y, 0) * Time.deltaTime * speed; transform.position = curPos + nexPos;
}

1. 좌우 콜라이더 부딛힘 떨리 조정 및 이동시 움직임의 지연 현상 보정

2. bool 값은 ~일 때!!

3. &&는 그리고 ||는 또는

private void Update()
{
keyMove();
}

private void keyMove()
{
float x = Input.GetAxisRaw("Horizontal");
if ((isTouchRight && x == 1) || (isTouchLeft && x == -1))
x = 0;
float y = Input.GetAxisRaw("Vertical");
if ((isTouchTop && y == 1) || (isTouchBottom && y == -1))
y = 0;
Vector3 curPos = transform.position; Vector3 nexPos = new Vector3(x, y, 0) * Time.deltaTime * speed; transform.position = curPos + nexPos;
}

편의상 키보드 이동 스크립트 따로 분류

 

이동 스크립트 따로 생성해서 싱글톤? 으로 받아오는 방법도 있는데 

이렇게 하면 캐릭터(플레이어)스크립트가 매우 단순해지고 추후 수정하는 것도 편할 것 같다...

일단 코드 정리는 나중에 하기로 하고!!

 

마우스 이동을 만들어 보자!!

마우스 이동은 클릭시 클릭위치로 이동방법도 있고

터치조이스틱을 만들어서 이동할 수도 있었다...

 

하지만 나는 화면 어디든 터치해서 드레그 했을때 오브젝트가 이동했으면...

하는 방법으로 찾아보고 연구해봤다..

public float leftBound, rightBound, topBound, bottomBound;

private void movePlayer()
{
dragPos = Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y)); transform.position = new Vector2(playerPos.x + (dragPos.x - touchPos.x), playerPos.y + (dragPos.y - touchPos.y));
if (transform.position.x < leftBound)
{ transform.position = new Vector2(leftBound, transform.position.y); }
if (transform.position.x > rightBound)
{ transform.position = new Vector2(rightBound, transform.position.y); }
if (transform.position.y < BottomBound)
{ transform.position = new Vector2(transform.position.x,  bottomBound); }
if (transform.position.y > TopBound)
{ transform.position = new Vector2(transform.position.x, topBound); }
}

마우스 이동 범위를 제한하기 위해서 바운드 설정

오른쪽, 왼쪽, 위, 아래 public float 을 띄워주고 언제는 조정할 수 있게 해봤다.

이제 인풋을 넣어 줘야한다. 그래야 마우스 따라 움직이니까~!

bool isDraging = false;

private void moveInput()
{
if (Input.GetMouseButtonDown(0))
{
isDraging = true;
touchPos = Camera.main.ScreenToWorldPoint(new Vector2(Input.mousePosition.x, Input.mousePosition.y)); playerPos = transform.position; }

else if (Input.GetMouseButtonUp(0))
{
isDraging = false;
}
}

isDraging = false --> 초기 값은 항상 요렇게

isDraging = true --> 마우스 터치시 드레그가 된다는 신호를 받고

터치하면 드레그가 true로 되고 이동, 터치를 하지 않으면 드레그는 다시 false 하여 정지

 

마지막으로 Update 함수에

private void Update()
{
movePlayer();
moveInput();
keyMove();
}

이렇게 하면 내가 원하는 오브젝트 이동 구현 완료!!

bound 값을 조정해서 오브젝트의 이동 범위를 지정할 수 있다!!

모바일용으로 계획하고 있기에 keyMove(); 는 삭제

728x90
반응형

공지사항

댓글