Priv's Blog

Programming Basics: Unit 2 - Basic Gameplay) Lesson 2.1 - Player Positioning 본문

Unity Learn/Pathway: Junior Programmer

Programming Basics: Unit 2 - Basic Gameplay) Lesson 2.1 - Player Positioning

Priv 2021. 2. 9. 23:31

출처

 

Lesson 2.1 - Player Positioning - Unity Learn

Overview: You will begin this unit by creating a new project for your second Prototype and getting basic player movement working. You will first choose which character you would like, which types of animals you would like to interact with, and which food y

learn.unity.com


 

 

1. 서언


(영상: 링크 참조)

 

Lesson 2.1 - Player Positioning - Unity Learn

Overview: You will begin this unit by creating a new project for your second Prototype and getting basic player movement working. You will first choose which character you would like, which types of animals you would like to interact with, and which food y

learn.unity.com


 


 

2. 프로토타입 2를 위해 새 프로젝트 생성하기

여러분이 가장 먼저 하셔야 하는 것은 새 프로젝트를 생성하고 Prototype 2 시작 파일을 추가하는 것입니다.


(영상: 링크 참조)

 

Lesson 2.1 - Player Positioning - Unity Learn

Overview: You will begin this unit by creating a new project for your second Prototype and getting basic player movement working. You will first choose which character you would like, which types of animals you would like to interact with, and which food y

learn.unity.com


 

  1. 유니티 허브(Unity Hub)를 여시고, 새로운 프로젝트를 "Prototype 2"라는 이름으로 여러분의 course 디렉터리 내에 생성해주세요.

  2. 링크를 클릭하여 Prototype 2 스타터 파일을 다운로드하시고, 유니티에 import 해주세요.

  3. Prototype 2 scene을 여시고 SampleScene은 저장하지 마시고 삭제해주세요.

  4. 유니티 에디터(Unity Editor) 우측 상단에서 레이아웃을 Default에서 여러분의 커스텀 레이아웃으로 변경해주세요.

 


 

3. 플레이어, 동물, 음식 추가하기

이제 scene에 플레이어, 동물, 음식을 포함한 여러분의 오브젝트들을 전부 배치해봅시다.

 


(영상: 링크 참조)

 

Lesson 2.1 - Player Positioning - Unity Learn

Overview: You will begin this unit by creating a new project for your second Prototype and getting basic player movement working. You will first choose which character you would like, which types of animals you would like to interact with, and which food y

learn.unity.com


 

  1. 원하신다면, Course Library > Materials에 있는 다른 머티리얼을 Ground 오브젝트로 드래그해주세요.

  2. Human 오브젝트 1개, Animals 오브젝트 3개, Food 오브젝트 1개를 Hierarchy로 드래그해주세요.

  3. 캐릭터 이름을 "Player"로 바꿔주시고, 동물과 음식을 여러분이 볼 수 있는 위치로 재배치해주세요.

  4. 음식의 XYZ 스케일을 조정하여 여러분이 쉽게 볼 수 있도록 만들어주세요.

 


 

4. 사용자의 Horizontal 입력 값 받기

여러분이 Player를 좌-우로 움직이게 만들고 싶으시다면, 사용자의 입력값을 변수로 받도록 만들어야 합니다.

 


(영상: 링크 참조)

 

Lesson 2.1 - Player Positioning - Unity Learn

Overview: You will begin this unit by creating a new project for your second Prototype and getting basic player movement working. You will first choose which character you would like, which types of animals you would like to interact with, and which food y

learn.unity.com

 


 

  1. 여러분의 Assets 폴더에서 "Scripts" 폴더를 생성하시고, 해당 폴더 내에 "PlayerController" 스크립트를 생성해주세요.

  2. 스크립트를 플레이어에 부착하시고, 스크립트 파일을 열어주세요.

  3. PlayerController.cs 상단에 public float horizontalInput이라고 새로 정의해주세요.

  4. Update() 안에, horizontalInput = Input.GetAxis("Horizontal")이라고 작성하시고, inspector 내에서 제대로 작동되는지 테스트해주세요.

 

public float horizontalInput;

void Update()
{
    horizontalInput = Input.GetAxis("Horizontal");
}

 


 

5. 플레이어를 좌-우로 움직이기

실제로 사용하는 horizontal 입력값을 플레이어의 좌측과 우측으로 변환해야 합니다.

 


(영상: 링크 참조)

 

Lesson 2.1 - Player Positioning - Unity Learn

Overview: You will begin this unit by creating a new project for your second Prototype and getting basic player movement working. You will first choose which character you would like, which types of animals you would like to interact with, and which food y

learn.unity.com


 

  1. public float speed = 10.0f;라고 새로 정의해주세요.

  2. Update() 내에 플레이어의 좌-우 움직임을 horizontalInputspeed로 변환해주세요.

 

public float horizontalInput;
public float speed = 10.0f;

void Update()
{
    horizontalInput = Input.GetAxis("Horizontal");
    transform.Translate(Vector3.right * horizontalInput * Time.deltaTime * speed);
}

 


 

6. 플레이어가 화면 밖을 벗어나지 않도록 만들기

이제 if 문을 사용하여 플레이어가 화면 옆으로 벗어나는 것을 방지해야 합니다.

 


(영상: 링크 참조)

 

Lesson 2.1 - Player Positioning - Unity Learn

Overview: You will begin this unit by creating a new project for your second Prototype and getting basic player movement working. You will first choose which character you would like, which types of animals you would like to interact with, and which food y

learn.unity.com


 

  1. Update() 내에 if문을 작성하여 만약 플레이어의 X 좌표가 포함된 값보다 작을 경우를 검사하게 만들어주세요.

  2. if문 안에 플레이어의 위치를 현재 위치로 설정하되, X 좌표값은 고정값으로 해주세요.

 

void Update() {
    if (transform.position.x < -10) {
        transform.position = new Vector3(-10, transform.position.y, transform.position.z);
    }
}

 


 

7. 코드와 변수들 정리하기

이제 여러분은 우측으로도 움직일 수 있도록 만들고, 코드를 정리해야 합니다.

 


(영상: 링크 참조)

 

Lesson 2.1 - Player Positioning - Unity Learn

Overview: You will begin this unit by creating a new project for your second Prototype and getting basic player movement working. You will first choose which character you would like, which types of animals you would like to interact with, and which food y

learn.unity.com


 

  1. 스크린의 우측 을 위해 윗 단계를 반복해주세요.

  2. xRange 변수를 새로 선언하고, 하드 코딩된 값들을 대체해주세요.

  3. 여러분의 코드에 주석을 채워주세요.

 

public float xRange = 10;

void Update()
{
    // Keep the player in bounds
    if (transform.position.x < -xRange)
    {
        transform.position = new Vector3(-xRange, transform.position.y, transform.position.z);
    }
    if (transform.position.x > xRange)
    {
        transform.position = new Vector3(xRange, transform.position.y, transform.position.z);
    }
}

 


 

8. 내용 복습


(영상: 링크 참조)

 

Lesson 2.1 - Player Positioning - Unity Learn

Overview: You will begin this unit by creating a new project for your second Prototype and getting basic player movement working. You will first choose which character you would like, which types of animals you would like to interact with, and which food y

learn.unity.com


 

- 새로 배운 기능들

  • 플레이어가 사용자의 좌, 우 키를 누르는 것에 따라 좌, 우로 움직일 수 있도록 만들기

  • 플레이어가 게임 화면 밖으로 나가지 않도록 만들기

 

- 새로 배운 개념과 기술들

  • 오브젝트 스케일 조정하기

  • If 문

  • 초과/미만 연산자

 

- 다음 시간에 학습할 내용

  • 여러분의 동물들을 먹이기 위한 무한한 양의 먹이들을 생성하고 던지는 방법에 대해 배워보겠습니다!

 


 


수고하셨습니다!


Comments