[DPad] 제작중

Unity 2022. 4. 14. 22:58
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Attach_PadArea : MonoBehaviour
{
    [Header("Camera")]
    [SerializeField]
    private Camera iUICamera;
    [Header("Pad")]
    [SerializeField]
    private Attach_DPad iDPad;
    private void Awake() {
        this.iDPad.Init();
        this.iDPad.SetCamera(this.iUICamera);
    }
    private void OnMouseDown()
    {
        // :: 마우스 포지션 Convert
        var mousePosition = 
            this.iUICamera.ScreenToWorldPoint(Input.mousePosition); 
        this.iDPad.transform.position = mousePosition;
        var LPos = this.iDPad.transform.localPosition;
        this.iDPad.transform.localPosition = new Vector3(LPos.x, LPos.y, 0);
    }
    private void OnMouseDrag() {
        this.iDPad.UpdateStick();
    }
    private void OnMouseUp() {
        this.iDPad.ResetStick();
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;

public class Attach_DPad : MonoBehaviour
{
    [Header("Stick")]
    [SerializeField]
    private Transform iStick;
    private Camera iUICamera;
    private float iRadius;
    public void Init()
    {
        // :: 반지름 구하기
        this.iRadius = this.GetComponent<RectTransform>().rect.width * 0.5f;
    }
    public void SetCamera(Camera _camera)
    {
        this.iUICamera = _camera;
    }
    [Header("Character")]
    [SerializeField]
    private NavMeshAgent iNavCharacter;
    public void UpdateStick()
    {
        // :: 드래그 포지션 확인
        var transformPosition =
            this.iUICamera.WorldToScreenPoint(this.transform.position);
        Vector2 positionDiff = new Vector2(
            Input.mousePosition.x - transformPosition.x,
            Input.mousePosition.y - transformPosition.y);

        // :: 반지름 이상 못 가게 처리
        // :: ClampMagnitude(벡터, 최대 길이)
        // :: >> 최대 길이로 한정된 벡터를 Return
        positionDiff = Vector2.ClampMagnitude(positionDiff, this.iRadius);

        // :: 스틱 위치 변경
        this.iStick.localPosition = positionDiff;

        // :: 스틱 위치 정규화
        Vector3 moveDirection = positionDiff.normalized;

        // :: 거리 비율 확인
        float distance = Vector3.Distance(Vector3.zero, 
            this.iStick.localPosition) / this.iRadius;

        // :: EXIT : 거리가 짧을 때
        if (distance <= 0.1f) return;

        // :: 로테이션 이동
        // :: Atan2(점1, 점2)
        // :: >> 두 점 사이의 각도를 Radian으로 출력
        // :: Rad2Deg
        // :: >> Radian에 곱하면 각도를 출력
        float currentAngle // : 현재 각도
            = this.iNavCharacter.transform.localRotation.eulerAngles.y;
        float degree = currentAngle +
                ((Mathf.Atan2(moveDirection.x, moveDirection.y)
                * Mathf.Rad2Deg) * Time.deltaTime * this.iNavCharacter.speed);
        this.iNavCharacter.transform.localRotation
            = Quaternion.Euler(new Vector3(0f, degree, 0));

        // :: 포지션 이동
        Vector3 movePos = new Vector3(moveDirection.x, 0, moveDirection.y);

        // :: 내비게이션 이동
        this.iNavCharacter.Move(
            this.iNavCharacter.transform.forward 
            * movePos.normalized.z * Time.deltaTime 
            * this.iNavCharacter.speed);
    }
    public void ResetStick()
    {
        this.iStick.localPosition = Vector3.zero;
    }
}

블로그 이미지

RIsN

,