하참이의 아이디어노트

Unity 강의 7일차 (1) - 시점 설정 본문

Unity/Unity 기초

Unity 강의 7일차 (1) - 시점 설정

하참이 2025. 1. 22. 03:01

본 강의는 다음 문서를 참고하여 제작하였습니다. 자세한 내용은 하단 링크를 참조하시거나 댓글로 질문 남겨주시면 성심성의껏 답변 드리겠습니다.

 

https://learn.unity.com/tutorial/4-1gang-sijeom-seoljeong?uv=2021.3&pathwayId=63ca4663edbc2a7be103183f&missionId=63c7676bedbc2a66daff99b1&projectId=63ca20deedbc2a7885b7ee97#

 

4.1강 - 시점 설정 - Unity Learn

개요: 먼저 새 프로토타입을 만들고 시작 파일을 다운로드합니다. 아름다운 섬과 하늘, 파티클 효과 등 다양한 에셋이 있으며, 모두 커스터마이즈할 수 있습니다. 이제 플레이어가 섬을 중심으

learn.unity.com

 


 

 

하단 링크 또는 상단 문서에서 압축파일을 다운 받은 뒤, 새 프로젝트에서 Import 합니다.

 

https://unity-connect-prd.storage.googleapis.com/20210507/c8291200-b024-4de8-8c86-2bfd0f323be3/Prototype%204%20-%20Starter%20Files.zip

 


Prototype 4 씬을 열고 저장하지 않은 채로 Sample Scene을 삭제합니다. 게임을 실행하여 파티클 효과를 확인합니다.

 

 

 

 

 

 

하이어라키에서 구체를 생성하고 이름을 Player로 설정합니다. Scale을 x, y, z 전부 1.5로 설정합니다.

 

 

 

 

 

 

Player에게 Rigidbody 컴포넌트를 부여합니다. 

 

Textures에 있는 아무 텍스쳐를 Player에게 드래그 합니다. 검은색이 눈에 띌 것 같습니다.

 

 

 

 

카메라 초점을 변경해보겠습니다. 빈 게임 오브젝트를 생성하고 이름을 Focal Point로 변경합니다.

 

그리고 카메라를 Focal Point의 자식 오브젝트로 드래그합니다.

 

 

 

새 스크립트 RotateCamera를 생성하고 스크립트 컴포넌트를 Focal Point에 부여합니다.

 

 

 

 

 

RotateCamera를 다음과 같이 작성합니다.

 

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

public class RotateCamera : MonoBehaviour
{
    public float rotationSpeed;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        transform.Rotate(Vector3.up, horizontalInput * rotationSpeed * Time.deltaTime);
    }
}



 

Input.GetAxis("Horizontal")을 이용하여 좌우 회전을 입력받습니다.

 

transform.Rotate를 이용하여 Focal Point를 회전시킵니다. 이 경우는 이전에 배웠던 것을 카메라의 관점 포인트에 적용한 응용이라 볼 수 있죠. 이를 이용해 카메라가 맵 주위를 회전하는 연출을 할 수 있습니다.

 

Unity Editor에서 RotateCamera 컴포넌트의 Rotation Speed 값을 변경해 속도를 조절합시다. 저는 50으로 하겠습니다.

 

 

 

 

이제 플레이어가 앞으로 움직일 수 있도록 해보겠습니다.

 

PlayerController 스크립트를 생성하고 Player 에게 부착한뒤, 다음과 같이 작성합니다.

 

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

public class PlayerController : MonoBehaviour
{
    private Rigidbody playerRb;
    public float speed = 5.0f;

    // Start is called before the first frame update
    void Start()
    {
        playerRb = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void Update()
    {
        float forwardInput = Input.GetAxis("Vertical");
        playerRb.AddForce(Vector3.forward * speed * forwardInput);
    }
}

 

 

AddForce를 이용하여 구체가 자연스럽게 굴러가도록 제작하였습니다. 설명은 생략하겠습니다.

 

 

하지만 전방과 후방만으로 움직이는 것은 저희가 원하는 방향이 아닙니다. 플레이어가 보는 방향, 즉 Focal Point 방향으로 움직이도록 수정하고 싶습니다. 다음과 같이 PlayerController를 수정합니다.

 

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

public class PlayerController : MonoBehaviour
{
    private Rigidbody playerRb;
    public float speed = 5.0f;
    private GameObject focalPoint;

    // Start is called before the first frame update
    void Start()
    {
        playerRb = GetComponent<Rigidbody>();
        focalPoint = GameObject.Find("Focal Point");
    }

    // Update is called once per frame
    void Update()
    {
        float forwardInput = Input.GetAxis("Vertical");
        playerRb.AddForce(focalPoint.transform.forward * speed * forwardInput);
    }
}

 

Focal Point 게임오브젝트를 찾아서 해당 게임 오브젝트의 위치 앞으로 움직이도록 합니다.