๊ธ€ ์ž‘์„ฑ์ž: ๋˜ฅํด๋ฒ .
๋ฐ˜์‘ํ˜•

์ด์ „ ํฌ์ŠคํŠธ


2019/06/25 - [Development/Game] - [Unity] ๋งˆ์šฐ์Šค ํด๋ฆญํ•œ ์ง€์ ์œผ๋กœ ์บ๋ฆญํ„ฐ ์ด๋™์‹œํ‚ค๊ธฐ

 

[Unity] ๋งˆ์šฐ์Šค ํด๋ฆญํ•œ ์ง€์ ์œผ๋กœ ์บ๋ฆญํ„ฐ ์ด๋™์‹œํ‚ค๊ธฐ

๋Œ€๋ถ€๋ถ„ ์œ ๋‹ˆํ‹ฐ ์บ๋ฆญํ„ฐ ์ด๋™ ์˜ˆ์ œ๋ฅผ ๋ณด๋ฉด ํ‚ค๋ณด๋“œ์˜ ์ž…๋ ฅ์„ ๋ฐ›์•„ ์ด๋™์‹œํ‚ค๋Š” ์˜ˆ์ œ๋“ค์ด ๋งŽ์€๋ฐ, ๋งˆ์šฐ์Šค๋กœ ์บ๋ฆญํ„ฐ๋ฅผ ์ด๋™์‹œํ‚ค๋Š” ์˜ˆ์ œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด๋ ค ํ•œ๋‹ค. ์ค€๋น„ ๊ณผ์ • ๊ฐ„๋‹จํ•œ ์บ๋ฆญํ„ฐ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ํ•˜๋‚˜ ์ƒ์„ฑํ•˜๊ณ  ์บ๋ฆญํ„ฐ ์˜ค..

cjwoov.tistory.com

 

์ค€๋น„ ๊ณผ์ •


์ค€๋น„ ๊ณผ์ •์€ ์ด์ „ ํฌ์ŠคํŠธ์™€ ๊ฐ™๋‹ค!

์šฐ๋ฆฌ์˜ ๋ชฉํ‘œ๋Š” ๋งˆ์šฐ์Šค ํœ ์„ ํ†ตํ•ด ์คŒ ์ธ/์•„์›ƒ์„ ๋‹ค๋ฃจ๊ณ , ๋งˆ์šฐ์Šค ์˜ค๋ฅธ์ชฝ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅธ ์ƒํƒœ์—์„œ ์›€์ง์ด๋ฉด ์นด๋ฉ”๋ผ๋ฅผ ํšŒ์ „์‹œ์ผœ ์ค„ ๊ฒƒ์ด๋‹ค!

์ด์ „ ํฌ์ŠคํŠธ์—์„œ MouseControl.cs๋ผ๋Š” ์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋งŒ๋“ค์–ด์„œ Main Camera ์˜ค๋ธŒ์ ํŠธ์— ์ถ”๊ฐ€์‹œ์ผœ ์ค„ ๊ฒƒ์ด๋‹ค!

 

์Šคํฌ๋ฆฝํŠธ


[CameraControl.cs]

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

public class CameraControl : MonoBehaviour
{
    public float rotateSpeed = 10.0f;
    public float zoomSpeed = 10.0f;

    private Camera mainCamera;

    void Start()
    {
        mainCamera = GetComponent<Camera>();
    }
    
    void Update()
    {
        Zoom();
        Rotate();
    }

    private void Zoom()
    {
        float distance = Input.GetAxis("Mouse ScrollWheel") * -1 * zoomSpeed;
        if(distance != 0)
        {
            mainCamera.fieldOfView += distance;
        }
    }

    private void Rotate()
    {
        if (Input.GetMouseButton(1))
        {
            Vector3 rot = transform.rotation.eulerAngles; // ํ˜„์žฌ ์นด๋ฉ”๋ผ์˜ ๊ฐ๋„๋ฅผ Vector3๋กœ ๋ฐ˜ํ™˜
            rot.y += Input.GetAxis("Mouse X") * rotateSpeed; // ๋งˆ์šฐ์Šค X ์œ„์น˜ * ํšŒ์ „ ์Šคํ”ผ๋“œ
            rot.x += -1 * Input.GetAxis("Mouse Y") * rotateSpeed; // ๋งˆ์šฐ์Šค Y ์œ„์น˜ * ํšŒ์ „ ์Šคํ”ผ๋“œ
            Quaternion q = Quaternion.Euler(rot); // Quaternion์œผ๋กœ ๋ณ€ํ™˜
            q.z = 0;
            transform.rotation = Quaternion.Slerp(transform.rotation, q, 2f); // ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํšŒ์ „
        }
    }
}

Update ํ•จ์ˆ˜์—์„œ ๋งค ํ”„๋ ˆ์ž„๋งˆ๋‹ค Zoomํ•จ์ˆ˜์™€ Rotateํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•ด์„œ ์คŒ๊ณผ ํšŒ์ „์„ ๋‹ค๋ฃฌ๋‹ค.

 

์คŒ

๋งˆ์šฐ์Šค ํœ ์˜ ์ž…๋ ฅ์„ ๋ฐ›์•„์„œ ์คŒ ์Šคํ”ผ๋“œ๋งŒํผ ๊ณฑํ•ด MainCamera์˜ fieldOfView ์†์„ฑ๊ฐ’์— ๋ˆ„์ ํ•ด์ค€๋‹ค.

์ด ์˜ˆ์ œ์—์„œ๋Š” ๋”ฑํžˆ ์คŒ ์ตœ๋Œ“๊ฐ’, ์ตœ์†Ÿ๊ฐ’์„ ์ •ํ•˜์ง€ ์•Š์•˜์ง€๋งŒ ์ œํ•œํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด fieldOfView์†์„ฑ์˜ ๊ฐ’์„ if๋ฌธ์œผ๋กœ ์ œ์–ดํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

ํšŒ์ „

Input.GetMouseButton(1) ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋‚ด๊ฐ€ ๋งˆ์šฐ์Šค ์˜ค๋ฅธ์ชฝ ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๊ณ  ์žˆ๋Š”์ง€ ํŒ๋‹จํ•œ๋‹ค.

๊ทธ๋‹ค์Œ ํ˜„์žฌ ๋งˆ์šฐ์Šค X์ขŒํ‘œ์™€ ๋งˆ์šฐ์Šค Y์ขŒํ‘œ๋ฅผ ๋ฐ›์•„์™€ ํ˜„์žฌ ๋‚ด ์นด๋ฉ”๋ผ์˜ ํšŒ์ „ ๊ฐ’์— ๋ˆ„์ ์‹œ์ผœ์ค€๋‹ค.

๊ทธ๋ฆฌ๊ณ  Quaternion.Slerp ํ•จ์ˆ˜๋ฅผ ํ†ตํ•ด ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ํšŒ์ „์‹œํ‚จ๋‹ค.

 

๊ฒฐ๊ณผ


๋ฐ˜์‘ํ˜•