다양한 기록

[Fortress Craft] Commander_Archer - 외형, 화살 나가는 방향 UI 본문

유니티 엔진/Fortress Craft

[Fortress Craft] Commander_Archer - 외형, 화살 나가는 방향 UI

라구넹 2025. 2. 5. 22:00

외형은 판타지 영화에 나올법한 엘프 궁수를 생각하면서 만듦

 

궁수 캐릭터는 바라보는 방향으로 화살을 쏨

그런데 2D라 바라보는 방향을 x, y축 상에서 나타내면 어딜 보는지 알기가 힘듦

⇒ 바라보는 방향을 화살표로 나타냄

화살표는 예전에 다른 게임(Parring Sword) 만들 때 그렸던 거 재활용

 

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

public class ArrowVector : MonoBehaviour
{
    public Vector2 TargetDirection { get; set; }
    Vector3 pos;

    private void Awake()
    {
        TargetDirection = Vector2.left;
    }

    private void FixedUpdate()
    {
        float angle = Mathf.Atan2(TargetDirection.y, TargetDirection.x) * Mathf.Rad2Deg;
        Quaternion FireRotation = Quaternion.AngleAxis(angle - 90, Vector3.forward);
        transform.rotation = FireRotation;

        float range = 0.3f;

        if( TargetDirection.y > 0.0f )
        {
            pos = new Vector3(TargetDirection.x * range, TargetDirection.y * range + 0.1f, 0.0f);
        }
        else
        {
            pos = new Vector3(TargetDirection.x * range, TargetDirection.y * range + 0.1f, 0.0f);
        }

        transform.position = transform.parent.position + pos;
    }   
}