말감둜그

ν¬λž˜ν”„ν†€ μ •κΈ€ 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