게임 엔진/Unity

[Unity] 전략게임 카메라 제작 방법

2020. 7. 6. 10:24
목차
  1. Strategy Camera

Strategy Camera

Unity에서 전략 시뮬레이션을 위한 카메라를 제작해야하는 경우가 있다.

먼저 Camera Rig로 사용할 GameObject를 하나 생성하고, 그 하위에 MainCamera를 놓는다.

 

그 후 MainCamera의 값을 다음과 같이 설정한다.

 

StrategyCameraController.cs

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

public class StrategyCameraController : MonoBehaviour
{
    public Transform followTransform;
    public Transform cameraTransform;

    public float normalSpeed;
    public float fastSpeed;

    public float movementSpeed;
    public float movementTime;
    public float rotationAmount;
    public Vector3 zoomAmount;

    public Vector3 newPosition;
    public Quaternion newRotation;
    public Vector3 newZoom;

    public Vector3 dragStartPostion;
    public Vector3 dragCurrentPostion;

    public Vector3 rotateStartPosition;
    public Vector3 rotateCurrentPosition;

    // Start is called before the first frame update
    void Start()
    {
        newPosition = transform.position;
        newRotation = transform.rotation;
        newZoom = cameraTransform.localPosition;
    }

    // Update is called once per frame
    void Update()
    {
        if (followTransform != null)
        {
            transform.position = followTransform.position;
        }
        else
        { 
            HandleMouseInput();
            HandleMovementInput();
        }

        if (Input.GetKeyDown(KeyCode.Escape))
        {
            followTransform = null;
        }
    }

    void HandleMouseInput()
    {
        if (Input.mouseScrollDelta.y != 0)
        {
            newZoom += Input.mouseScrollDelta.y * zoomAmount;
        }

        if(Input.GetMouseButtonDown(0))
        {
            Plane plane = new Plane(Vector3.up, Vector3.zero);

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            float entry;

            if (plane.Raycast(ray, out entry))
            {
                dragStartPostion = ray.GetPoint(entry);
            }
        }

        if (Input.GetMouseButton(0))
        {
            Plane plane = new Plane(Vector3.up, Vector3.zero);

            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            float entry;

            if (plane.Raycast(ray, out entry))
            {
                dragCurrentPostion = ray.GetPoint(entry);

                newPosition = transform.position + dragStartPostion - dragCurrentPostion;
            }
        }

        if (Input.GetMouseButtonDown(2))
        {
            rotateStartPosition = Input.mousePosition;
        }
        if (Input.GetMouseButton(2))
        {
            rotateCurrentPosition = Input.mousePosition;

            Vector3 difference = rotateStartPosition - rotateCurrentPosition;

            rotateStartPosition = rotateCurrentPosition;

            newRotation *= Quaternion.Euler(Vector3.up * (-difference.x / 5f));
        }

    }

    void HandleMovementInput()
    {
        if (Input.GetKey(KeyCode.LeftShift))
        {
            movementSpeed = fastSpeed;
        }
        else
        {
            movementSpeed = normalSpeed;
        }

        if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow))
        {
            newPosition += (transform.forward * movementSpeed);
        }

        if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow))
        {
            newPosition += (transform.forward * -movementSpeed);
        }

        if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
        {
            newPosition += (transform.right * movementSpeed);
        }

        if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
        {
            newPosition += (transform.right * -movementSpeed);
        }

        if (Input.GetKey(KeyCode.Q))
        {
            newRotation *= Quaternion.Euler(Vector3.up * rotationAmount);
        }

        if (Input.GetKey(KeyCode.E))
        {
            newRotation *= Quaternion.Euler(Vector3.up * -rotationAmount);
        }

        if (Input.GetKey(KeyCode.R))
        {
            newZoom += zoomAmount;
        }

        if (Input.GetKey(KeyCode.F))
        {
            newZoom -= zoomAmount;
        }

        transform.position = Vector3.Lerp(transform.position, newPosition, Time.deltaTime * movementTime);
        transform.rotation = Quaternion.Lerp(transform.rotation, newRotation, Time.deltaTime * movementTime);
        cameraTransform.localPosition = Vector3.Lerp(cameraTransform.localPosition, newZoom, Time.deltaTime * movementTime);

    }
}

'게임 엔진 > Unity' 카테고리의 다른 글

[Unity] Hp Bar 만들기  (0) 2020.07.13
[Unity] 글로벌 이벤트 관리 방법  (1) 2020.07.06
[Unity] 동적타임에 NavMesh 생성하기  (0) 2020.07.02
[Unity] 커스텀 에디터 제작 방법 (Editor Customize)  (0) 2020.06.30
[Unity] 길찾기 시스템을 통한 이동과 애니메이션 컨트롤러  (3) 2020.06.29
  1. Strategy Camera
'게임 엔진/Unity' 카테고리의 다른 글
  • [Unity] Hp Bar 만들기
  • [Unity] 글로벌 이벤트 관리 방법
  • [Unity] 동적타임에 NavMesh 생성하기
  • [Unity] 커스텀 에디터 제작 방법 (Editor Customize)
AlgorFati
AlgorFati
algorfati@gmail.com
AlgorFati
AlgorFati의 개발 기록
AlgorFati
전체
오늘
어제
  • 분류 전체보기 (111)
    • 경제 (0)
    • 서비스 운영 (3)
    • 시행착오 (1)
    • 프로그래밍 (50)
      • CS 개념 (3)
      • 알고리즘 (32)
      • C++ (7)
      • C# (3)
      • OS (1)
      • Python (4)
    • 사업 (0)
    • Back End (1)
    • 생활 (1)
    • 정보 보안 (0)
    • 게임 개발 (1)
    • Tool (2)
    • 기술 (0)
    • AI (2)
    • 게임 엔진 (50)
      • Unreal (36)
      • Unity (14)

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • Collision
  • pathfinding
  • property editor
  • 알고리즘
  • customize
  • 프로그래머스
  • 카카오
  • geom
  • Nullable
  • Event Handling
  • aggregate geom
  • Editor
  • TickGroup
  • debug draw
  • 코딩테스트
  • 비동기 프로그래밍
  • Serialize
  • Slate
  • 대기업
  • navmesh
  • ml agent
  • unity
  • 취업
  • data asset
  • Animation
  • 불변성
  • asset to editor
  • Unreal
  • hp bar
  • c#

최근 댓글

최근 글

hELLO · Designed By 정상우.
AlgorFati
[Unity] 전략게임 카메라 제작 방법
상단으로

티스토리툴바

단축키

내 블로그

내 블로그 - 관리자 홈 전환
Q
Q
새 글 쓰기
W
W

블로그 게시글

글 수정 (권한 있는 경우)
E
E
댓글 영역으로 이동
C
C

모든 영역

이 페이지의 URL 복사
S
S
맨 위로 이동
T
T
티스토리 홈 이동
H
H
단축키 안내
Shift + /
⇧ + /

* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.