박민혁의 개발

의외로 심플한 블랙홀 구현 본문

TIL

의외로 심플한 블랙홀 구현

박민혁_kog 2023. 12. 19. 16:56
    List<GameObject> target=new List<GameObject>();
    float time = 0;
    Vector3 dir;
    private void Update()
    {
        time += Time.deltaTime;

        foreach (GameObject star in target)
        {
            if (star != null && star.GetComponent<Rigidbody2D>()) 
            {
                dir = this.transform.position - star.transform.position;
                dir = dir * 10f;
                star.GetComponent<Rigidbody2D>().AddForce(dir);
            }
        }
        if (time >= 3) 
        {
            Destroy(this.gameObject);
        }
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        target.Add(collision.gameObject);
    }

    private void OnTriggerExit2D(Collider2D collision)
    {
        target.Remove(collision.gameObject);
    }

위 코드는 콜라이더 엔터로 구별 했지만 레이캐스트서클로 사방에 레이를 쏴서 거리안에 부딪힌 대상으로 구별 하는 방법도 있다.

'TIL' 카테고리의 다른 글

유니티 언어 설정  (1) 2024.06.14
점멸 구현  (0) 2023.12.19
상태이상 구현하기 그런데 최적화를 곁들인  (0) 2023.12.19
데바데 스킬 체크 구현  (1) 2023.12.19
유니티 유도 미사일  (1) 2023.12.19