다양한 기록

[Fortress Craft] Commander_Magician - 기본 공격, 스킬1, 스킬2 본문

유니티 엔진/Fortress Craft

[Fortress Craft] Commander_Magician - 기본 공격, 스킬1, 스킬2

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

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Fusion;
using NetworkRigidbody2D = Fusion.Addons.Physics.NetworkRigidbody2D;

namespace Agit.FortressCraft
{
    public class MagicianSpell : NetworkBehaviour
    {
        public Transform Target { get; set; }
        private NetworkRigidbody2D _rb;
        public float SpellSpeed { get; set; }
        public NetworkPrefabId ID { get; set; }
        public bool Fired { get; set; }
        public string OwnType { get; set; }
        private TickTimer destroyTimer;
        private MagicianSpellAttackCollider attackCollider;


        public override void Spawned()
        {
            _rb = GetComponent<NetworkRigidbody2D>();
            attackCollider = GetComponent<MagicianSpellAttackCollider>();
            Target = null;
            Invoke("DestroySelf", 1.3f);
        }

        public override void FixedUpdateNetwork()
        {
            Target = null;

            Collider2D[] cols = Physics2D.OverlapCircleAll(
                        new Vector2(transform.position.x, transform.position.y), 3.0f);

            foreach (Collider2D col in cols)
            {
                if (col.tag.StartsWith("Unit"))
                {
                    if (!col.CompareTag("Unit_" + OwnType))
                    {
                        if( col.transform.parent != null )
                        {
                            Target = col.transform.parent;
                        }
                        else
                        {
                            Target = col.transform;
                        }
                        
                        break;
                    }
                }
            }

            if (Target != null)
            {
                _rb.Rigidbody.velocity = ( Target.position - transform.position ).normalized * SpellSpeed;
            }
            else
            {
                Debug.Log("Magic Can't Move");
                _rb.Rigidbody.velocity = Vector2.zero;
            }

        }

        public void DestroySelf()
        {
            Destroy(this.gameObject);
        }
    }
}

적을 추적하는 에너지 구체

파티클 시스템을 이용해서 만들었음

적이 없으면 가만히 있음

 

스킬1

화염 방사, 드래곤의 브레스 같은 느낌

파티클 시스템을 이용해서 만들었음

공격 판정은 Animation에서 컬라이더 켜는 걸로 설정

 

* 플레이어를 공격하지 못하는 버그

트리거 처리는 RigidBody가 물체 둘 중 하나는 있어야 가능함

근데 플레이어에 RigidBody (NetworkRigidBody2D)가 아니라 NetworkCharacterController를 씀

=> 트리거에 같이 붙여줬음

 

* 스킬2 회전 로테이션 안되는 버그

RPC 써서 해결

 

 

스킬2

주기적으로 적을 공격하는 설치기

5초 지속 10초 쿨타임, 공격 주기 0.5초

파티클 시스템을 이용해 직접 만들었고, 파티클에 들어가는 이미지는 직접 그렸음

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Fusion;
using NetworkRigidbody2D = Fusion.Addons.Physics.NetworkRigidbody2D;

namespace Agit.FortressCraft
{
    public class MagicianTwinkle : NetworkBehaviour
    {
        public NetworkPrefabId ID { get; set; }
        private TickTimer onOfftimer;
        private TickTimer destroyTimer;
        private BoxCollider2D trigger;


        public override void Spawned()
        {
            trigger = GetComponentInChildren<BoxCollider2D>();
            onOfftimer = TickTimer.CreateFromSeconds(Runner, 0.01f);
            destroyTimer = TickTimer.CreateFromSeconds(Runner, 5.0f);
        }

        public override void FixedUpdateNetwork()
        {
            if( destroyTimer.Expired( Runner ) )
            {
                Destroy(this.gameObject);
            }

            if( onOfftimer.Expired(Runner) )
            {
                trigger.enabled = !trigger.enabled;
                onOfftimer = TickTimer.CreateFromSeconds(Runner, 0.5f);
            }

        }
    }
}

0.5초 주기로 트리거를 껐다 켰다 반복함