Unity2D

Unity) [유니티 2D] Skill(Spell) Indicator 구현

HSH12345 2023. 3. 14. 00:05
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class JoystickSkillDir : Joystick
{
    private bool isSlow;
    public System.Action shootSkill;
    public bool IsSkillOperated { get { return IsDraged; } }
    public bool IsSlow { get { return isSlow; } }
    
    public override void OnPointerUp(PointerEventData eventData)
    {
        float dist = Vector2.Distance(Vector2.zero, this.Input);
        //Debug.Log(dist);
        if(dist > 40)
        {
            this.shootSkill();
        }
        else
        {
            Debug.Log("CancelSkill");
        }
        base.OnPointerUp(eventData);        
        this.frame.gameObject.SetActive(false);

        this.isSlow = false;
    }

    public override void OnDrag(PointerEventData eventData)
    {
        base.OnDrag(eventData);
        this.frame.gameObject.SetActive(true);
    }

    public override void OnPointerDown(PointerEventData eventData)
    {
        base.OnPointerDown(eventData);

        this.isSlow = true;
    }
}
    public void SetSkill()
    {
        this.skillType = this.gunShell.skillType;
        if (this.joystickSkillDir.IsSkillOperated)
        {
            this.indicators[(int)this.skillType].gameObject.SetActive(true);
            if ((int)this.skillType == 2) this.bg.SetActive(true);
        }
        else
        {
            this.indicators[(int)this.skillType].gameObject.SetActive(false);
            if ((int)this.skillType == 2)
            {
                this.indicators[(int)this.skillType].gameObject.transform.position = this.transform.position;
                this.bg.SetActive(false);
            }
        }

        if ((int)this.skillType < 2)
        {
            for (int i = 0; i < indicators.Length; i++)
            {
                if (this.joystickSkillDir.Input == Vector3.zero) return;
                var angle = Mathf.Atan2(this.joystickSkillDir.Vertical, this.joystickSkillDir.Horizontal) * Mathf.Rad2Deg;
                var lookRotation = Quaternion.Euler(angle * Vector3.forward);
                this.indicators[i].transform.rotation = lookRotation;
            }
        }
        else
        {
            float range = 5;
            this.skillDir = new Vector2(this.joystickSkillDir.Horizontal, this.joystickSkillDir.Vertical);
            var mag = this.indicators[(int)this.skillType].transform.localPosition.magnitude;

            if (mag < range)
            {
                this.indicators[(int)this.skillType].transform.localPosition = this.skillDir / 30;
            }
            else
            {
                this.indicators[(int)this.skillType].transform.localPosition = this.skillDir.normalized * range;
            }

            //Debug.Log(this.indicators[(int)this.skillType].transform.localPosition);

            //Debug.Log(dist);
        }

 DragUp의 위치에 따라 스킬이 취소되거나 실행된다.

 

 

 

 Indicator의 범위를 지정해두었으나 중간중간 범위 밖으로 튀어나갔다 들어오는 상황 수정해야함

 

 

스킬 사용에 따라 슬로우모션이 실행되지만 구현만 해둔 상태이고 수치는 조절해야한다.