๊ด€๋ฆฌ ๋ฉ”๋‰ด

๋ง๊ฐ๋กœ๊ทธ

ํฌ๋ž˜ํ”„ํ†ค ์ •๊ธ€ WEEK12 DAY 93 - ์œ ๋‹ˆํ‹ฐ ํ„ฐ์น˜ ํŒจ๋„๋กœ ์นด๋ฉ”๋ผ ์กฐ์ž‘ & ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ ์šฉ ๋ณธ๋ฌธ

Krafton jungle

ํฌ๋ž˜ํ”„ํ†ค ์ •๊ธ€ WEEK12 DAY 93 - ์œ ๋‹ˆํ‹ฐ ํ„ฐ์น˜ ํŒจ๋„๋กœ ์นด๋ฉ”๋ผ ์กฐ์ž‘ & ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ ์šฉ

habbn 2024. 4. 8. 22:46
728x90
๐Ÿ“†2024.04.08

1. ์œ ๋‹ˆํ‹ฐ ํ„ฐ์น˜ ํŒจ๋„๋กœ ์นด๋ฉ”๋ผ ์กฐ์ž‘ & ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ ์šฉ 
2. UnityYAMLMerge ์„ค์น˜
3. ๋ฐฑ์ค€

 

์–ด์ œ ์นด๋ฉ”๋ผ ํšŒ์ „์ด ํ”Œ๋ ˆ์ด์–ด ์ค‘์‹ฌ์œผ๋กœ ์•ˆ๋˜๊ณ  ์˜ํ™” ์นด๋ฉ”๋ผ ๋งˆ๋ƒฅ ์กฐ์ž‘๋˜์–ด์„œ ์˜ค๋Š˜ ๋‹ค์‹œ ์ œ๋Œ€๋กœ ์ˆ˜์ •ํ•˜์˜€๋‹ค.

 

์ด ๋ถ„ ์œ ํŠœ๋ธŒ๋ฅผ ๋ณด๋ฉด์„œ ์ฐธ๊ณ ํ•˜์˜€๋‹ค.

https://www.youtube.com/watch?v=4611qmBWTC0

 

 

MainCamera.cs

using UnityEngine;

public class MainCamera : MonoBehaviour
{
    public Transform target;
    public float followspeed = 15f; //๋”ฐ๋ผ๊ฐ€๋Š” ์†๋„
    public float sensitive = 7f;  //๊ฐ๋„
    public float clampAngle = 50f;  //์นด๋ฉ”๋ผ ํšŒ์ „ ๊ฐ๋„์˜ ์ œํ•œ

    private float rotX;
    private float rotY;

    public Transform realCamera;
    public Vector3 dirNormalized;   //๋ฐฉํ–ฅ๋ฒกํ„ฐ
    public Vector3 finalDir;    //์ตœ์ข… ๋ฐฉํ–ฅ
    public float minDistance;   //์ตœ์†Œ ๊ฑฐ๋ฆฌ
    public float maxDistance;   //์ตœ๋Œ€ ๊ฑฐ๋ฆฌ
    public float finalDistance; //์ตœ์ข… ๊ฑฐ๋ฆฌ
    public float smoothness = 10f;

    public FixedTouchField touchField;
    public bool enableMobile = false;

    void Start()
    {
        rotX = transform.localRotation.eulerAngles.x;
        rotY = transform.localRotation.eulerAngles.y;

        dirNormalized = realCamera.localPosition.normalized;
        
        //magnitude๋Š” ๋ฒกํ„ฐ์˜ ๊ธธ์ด(ํฌ๊ธฐ)๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ
        finalDistance = realCamera.localPosition.magnitude;
    }

    void Update()
    {
        if (enableMobile)
        {
            rotY += touchField.TouchDist.x * sensitive * Time.deltaTime;
            rotX -= touchField.TouchDist.y * sensitive * Time.deltaTime;
        }
        else
        {
            rotY += Input.GetAxis("Mouse X") * sensitive * Time.deltaTime;
            rotX -= Input.GetAxis("Mouse Y") * sensitive * Time.deltaTime;
        }

        rotX = Mathf.Clamp(rotX, -clampAngle, clampAngle);  //๊ฐ๋„์˜ ๋ฒ”์œ„
        Quaternion rot = Quaternion.Euler(rotX, rotY, 0);   //์นด๋ฉ”๋ผ์˜ ํšŒ์ „
        transform.rotation = rot;
    }

    void LateUpdate()
    {
        transform.position = Vector3.Lerp(transform.position, target.position, followspeed * Time.deltaTime);
		
        //transform.TransformPoint() ๋กœ์ปฌ ์ขŒํ‘œ๊ณ„์—์„œ ์ง€์ •๋œ ์ง€์ ์„ ์›”๋“œ ์ขŒํ‘œ๊ณ„๋กœ ๋ณ€ํ™˜
        finalDir = transform.TransformPoint(dirNormalized * maxDistance);

        RaycastHit hit;
        
        //์นด๋ฉ”๋ผ์˜ ํ˜„์žฌ ์œ„์น˜์™€ ์ตœ์ข… ์œ„์น˜ ์‚ฌ์ด์˜ ์žฅ์• ๋ฌผ์ด ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌ
        //์žฅ์• ๋ฌผ์ด ์žˆ์œผ๋ฉด hit ๋ณ€์ˆ˜์— ์ •๋ณด๊ฐ€ ์ €์žฅ
        if(Physics.Linecast(transform.position, finalDir, out hit))
        {	
            //minDistance์™€ maxDistance ์‚ฌ์ด์˜ ๊ฐ’์„ ์œ ์ง€ํ•˜๋„๋ก ๊ฑฐ๋ฆฌ๋ฅผ Clampํ•จ
            finalDistance = Mathf.Clamp(hit.distance, minDistance, maxDistance);
        }
        else
        {
            finalDistance = maxDistance;
        }
		
        //Lerp ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹ค์ œ ์นด๋ฉ”๋ผ์˜ ๋กœ์ปฌ ์œ„์น˜๋ฅผ ๋ฐฉํ–ฅ ๋ฒกํ„ฐ์™€ ์ตœ์ข… ๊ฑฐ๋ฆฌ์— ๋งž๊ฒŒ ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ์ด๋™
        realCamera.localPosition = Vector3.Lerp(realCamera.localPosition, dirNormalized * finalDistance, Time.deltaTime * smoothness);
    }
}

 

Mathf.Clamp ํ•จ์ˆ˜๋Š” ์ฃผ์–ด์ง„ ๊ฐ’์˜ ๋ฒ”์œ„๋ฅผ ์ง€์ •๋œ ์ตœ์†Œ๊ฐ’๊ณผ ์ตœ๋Œ“๊ฐ’ ์‚ฌ์ด๋กœ ์ œํ•œํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค. 

TransformPoint() ํ•จ์ˆ˜๋Š” ๋กœ์ปฌ ์ขŒํ‘œ๊ณ„์—์„œ ์ง€์ •๋œ ์ง€์ ์„ ์›”๋“œ ์ขŒํ‘œ๊ณ„๋กœ ๋ณ€ํ™˜ํ•˜๋Š”๋ฐ ์‚ฌ์šฉ๋œ๋‹ค.

 

 public Vector3 TransformPoint(Vector3 position);

 

โ€ข position์€ ํ˜„์žฌ ๊ฐ์ฒด์˜ ๋กœ์ปฌ ์ขŒํ‘œ๊ณ„์—์„œ์˜ ์œ„์น˜

โ€ข TransformPoint๋Š” ํ•ด๋‹น ์ง€์ ์„ ์›”๋“œ ์ขŒํ‘œ๊ณ„๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

์ด ๋ฉ”์„œ๋“œ๋Š” ์ฃผ๋กœ ๊ฐ์ฒด์˜ ๋กœ์ปฌ ์ขŒํ‘œ๊ณ„์—์„œ ํŠน์ • ์ง€์ ์„ ์›”๋“œ ์ขŒํ‘œ๊ณ„๋กœ ๋ณ€ํ™˜ํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค. ์ฃผ๋กœ ํŠน์ • ์ง€์ ์˜ ์ ˆ๋Œ€ ์œ„์น˜๋ฅผ ์–ป๊ฑฐ๋‚˜, ํ•ด๋‹น ์ง€์ ์„ ๋‹ค๋ฅธ ๊ฐ์ฒด์˜ ๊ณต๊ฐ„์—์„œ ์‚ฌ์šฉํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค.

 

 

 

๊ทธ๋ฆฌ๊ณ  ๋ฐ‹๋ฐ‹ํ•œ ํ”Œ๋ ˆ์ด์–ด ์ด๋™์— ์• ๋‹ˆ๋ฉ”์ด์…˜์„ ์ž…ํ˜€์ฃผ์—ˆ๋‹ค.

 

PlayerController.cs

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

public class PlayerController : MonoBehaviour
{
    public bool enableMobile = false;
    
    [SerializeField]
    private float smoothRotationTime;   //target ๊ฐ๋„๋กœ ํšŒ์ „ํ•˜๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„
    [SerializeField]
    private float smoothMoveTime;   //target ์†๋„๋กœ ๋ฐ”๋€Œ๋Š”๋ฐ ๊ฑธ๋ฆฌ๋Š” ์‹œ๊ฐ„
    [SerializeField]
    private float moveSpeed;
    private float rotationVelocity;
    private float speedVelocity;
    private float currentSpeed;
    private float targetSpeed;

    Transform cameraTransform;
    public VariableJoystick joystick;
    Animator anim;

    void Awake()
    {
        anim = GetComponent<Animator>();
        cameraTransform = Camera.main.transform;
    }

    void Update()
    {
        Vector2 input = Vector2.zero;

        if (enableMobile)
        {
            input = new Vector2(joystick.input.x, joystick.input.y);
        }
        else
        {
            input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
        }

        anim.SetBool("IsWalk", input != Vector2.zero);

        Vector2 inputDir = input.normalized;

        //์›€์ง์ž„์„ ๋ฉˆ์ท„์„ ๋•Œ ๋‹ค์‹œ ์ฒ˜์Œ ๊ฐ๋„๋กœ ๋Œ์•„๊ฐ€๋Š” ๊ฑธ ๋ง‰๊ธฐ ์œ„ํ•จ
        if (inputDir != Vector2.zero)
        {
            float rotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraTransform.eulerAngles.y;
            transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, rotation, ref rotationVelocity, smoothRotationTime);
        }
        
        //์ž…๋ ฅ ๋ฐฉํ–ฅ์— ๋”ฐ๋ฅธ ๋ชฉํ‘œ ์†๋„
        targetSpeed = moveSpeed * inputDir.magnitude;
        //ํ˜„์žฌ ์†๋„๋ฅผ ๋ชฉํ‘œ ์†๋„๋กœ ๋ถ€๋“œ๋Ÿฝ๊ฒŒ ์กฐ์ ˆ
        currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedVelocity, smoothMoveTime);
        //ํ˜„์žฌ ์Šคํ”ผ๋“œ์—์„œ ํƒ€๊ฒŸ ์Šคํ”ผ๋“œ๊นŒ์ง€ smoothMoveTime ๋™์•ˆ ๋ณ€ํ•œ๋‹ค.
        transform.Translate(transform.forward * currentSpeed * Time.deltaTime, Space.World);
        
    }
    void OnTriggerStay(Collider other) {
        if (other.gameObject.tag == "NoticeBoard") {
            Debug.Log("NoticeBoard");
        }
    }
}

 

 

 

ํ˜„์žฌ๊นŒ์ง€ ํ”Œ๋ ˆ์ด์–ด ์ด๋™ & ์นด๋ฉ”๋ผ ํšŒ์ „์€ ์™„๋ฃŒ๋œ ์ƒํƒœ์ด๋‹ค~

์ถ”๊ฐ€๋กœ ๋กœ๊ทธ์ธ & ํšŒ์›๊ฐ€์ž… & ์บ๋ฆญํ„ฐ ์„ ํƒ ์ฐฝ์€ ๊ฐ™์€ ํŒ€์›์ด ์บ๋ฆฌํ•ด๋ฒ„๋ ค์„œ

๋‚ด์ผ๋ถ€ํ„ฐ ๊ฒŒ์‹œํŒ ์ž‘์„ฑ ๋ถ€๋ถ„ ๊ตฌํ˜„ ๊ณ„ํš์ด๋‹ค.

 

๋ฐฑ์ค€

 

์†Œ์ˆ˜ ๊ฒฝ๋กœ  ๋ฌธ์ œ๋ฅผ ํ’€๋ ค๊ณ  ํ–ˆ๋”๋‹ˆ ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด๋ฅผ ์•Œ์•„์•ผ ํ•œ๋‹ค๊ณ  ํ•ด์„œ ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด ๊ณต๋ถ€๋ฅผ ํ•˜์˜€๋‹ค.

์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด๋Š” ๋‹ค์ˆ˜์˜ ์ž์—ฐ์ˆ˜์— ๋Œ€ํ•˜์—ฌ ์†Œ์ˆ˜ ์—ฌ๋ถ€๋ฅผ ํŒ๋ณ„ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€ํ‘œ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค.

 

[๋ฐฑ์ค€] 2960๋ฒˆ ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด (ํŒŒ์ด์ฌ)

์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด ๋‹ค์ˆ˜์˜ ์ž์—ฐ์ˆ˜์— ๋Œ€ํ•˜์—ฌ ์†Œ์ˆ˜ ์—ฌ๋ถ€๋ฅผ ํŒ๋ณ„ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ๋Œ€ํ‘œ์ ์ธ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋‹ค. ์—๋ผํ† ์Šคํ…Œ๋„ค์Šค์˜ ์ฒด๋Š” N๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ๋ชจ๋“  ์†Œ์ˆ˜๋ฅผ ์ฐพ์„ ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ๋™์ž‘ ๊ณผ

habbn-unitystudy.tistory.com

 

 

 

728x90