Skip to content

develop Cross

Seong Hyeok OH edited this page Jul 5, 2023 · 2 revisions

준비

프리팹

image image

스크립트

  • Cross

using UnityEngine;

public class Cross : Weapon
{
    [SerializeField] Animator mAnimator;
    private float mCoolTimer = 0;
    private bool mbUsed = false;
    private Vector3 mDirection;

    private void Start()
    {
        mAnimator = GetComponent();
    }
    private void FixedUpdate()
    {
        if (!mbUsed) return;

        Rigidbody2D rb = transform.GetComponent();
        rb.AddForce(-mDirection, ForceMode2D.Force);

        if (rb.velocity.magnitude <= 0.001)
        {
            transform.Rotate(0f, 0f, 180f);
        }
    }
    public override void Attack()
    {
        GameObject objPre = IsEvoluction() ? SkillFiringSystem.instance.evolutionWeaponPrefabs[WeaponIndex] : SkillFiringSystem.instance.weaponPrefabs[WeaponIndex];

        float cooldown = WeaponTotalStats[(int)Enums.EWeaponStat.Cooldown];
        float speed = WeaponTotalStats[(int)Enums.EWeaponStat.ProjectileSpeed];

        mCoolTimer += Time.deltaTime;
        if (mCoolTimer >= cooldown)
        {
            int numProjectiles = (int)WeaponTotalStats[(int)Enums.EWeaponStat.Amount];
            for (int i = 0; i < numProjectiles; i++)
            {
                // 무기 생성
                GameObject newObs = Instantiate(objPre, GameObject.Find("SkillFiringSystem").transform);
                newObs.transform.position = GameManager.instance.Player.transform.position;
                Cross newCross = newObs.GetComponent();
                newCross.mbUsed = true;
                // 무기 방향
                Vector3 direction = FindClosestEnemyDirection();
                newCross.mDirection = direction;
                float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
                newObs.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
                // 무기 발사
                Rigidbody2D rb = newObs.GetComponent();
                rb.velocity = direction * speed * 3;
            }
            mCoolTimer = 0;
        }
    }
    private Vector3 FindClosestEnemyDirection()
    {
        GameObject closestEnemy = FindClosestEnemy();
        if (closestEnemy != null)
        {
            Vector3 direction = closestEnemy.transform.position - transform.position;
            return direction.normalized;
        }
        else
        {
            return Vector3.right;
        }
    }
    private GameObject FindClosestEnemy()
    {
        GameObject[] enemies = GameObject.FindGameObjectsWithTag("Monster");
        GameObject closestEnemy = null;
        float closestDistance = Mathf.Infinity;

        foreach (GameObject enemy in enemies)
        {
            float distance = Vector3.Distance(transform.position, enemy.transform.position);

            if (distance < closestDistance)
            {
                closestEnemy = enemy;
                closestDistance = distance;
            }
        }
        return closestEnemy;
    }
}

  • 핵심

    private void FixedUpdate()
    {
        if (!mbUsed) return;

        Rigidbody2D rb = transform.GetComponent();
        rb.AddForce(-mDirection, ForceMode2D.Force);

        if (rb.velocity.magnitude <= 0.001)
        {
            transform.Rotate(0f, 0f, 180f);
        }
    }
    public override void Attack()
    {
        GameObject objPre = IsEvoluction() ? SkillFiringSystem.instance.evolutionWeaponPrefabs[WeaponIndex] : SkillFiringSystem.instance.weaponPrefabs[WeaponIndex];

        float cooldown = WeaponTotalStats[(int)Enums.EWeaponStat.Cooldown];
        float speed = WeaponTotalStats[(int)Enums.EWeaponStat.ProjectileSpeed];

        mCoolTimer += Time.deltaTime;
        if (mCoolTimer >= cooldown)
        {
            int numProjectiles = (int)WeaponTotalStats[(int)Enums.EWeaponStat.Amount];
            for (int i = 0; i < numProjectiles; i++)
            {
                // 무기 생성
                GameObject newObs = Instantiate(objPre, GameObject.Find("SkillFiringSystem").transform);
                newObs.transform.position = GameManager.instance.Player.transform.position;
                Cross newCross = newObs.GetComponent();
                newCross.mbUsed = true;
                // 무기 방향
                Vector3 direction = FindClosestEnemyDirection();
                newCross.mDirection = direction;
                float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
                newObs.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
                // 무기 발사
                Rigidbody2D rb = newObs.GetComponent();
                rb.velocity = direction * speed * 3;
            }
            mCoolTimer = 0;
        }
    }

Clone this wiki locally