Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

mooni

[Unity] 3차 Study : GimmickBlock Script 본문

Unity

[Unity] 3차 Study : GimmickBlock Script

mooni_ 2024. 3. 11. 20:05

[GimmickBlock]

변수 선언부

public float length = 0.0f;    //탐지 거리
public bool isDelete = false;    //낙하 후 제거 여부
bool isFell = false;    //낙하 플래그
float fadeTime = 0.5f;    //페이드아웃 시간

 

 


Start()

void Start()
{
    Rigidbody2D rbody = GetComponent<Rigidbody2D>();
    //얘가 왜 Start에 있음...?
    rbody.bodyType = RigidbodyType2D.Static;
    //초기 위치가 공중이라 떨어지지 않도록 bodyType을 static으로 설정
}

Rigidbody2D 선언이 Start에 있어서 30분동안 토론함

왜 이렇게 쓴거지???????

 

 


Update()

void Update()
{
    GameObject player = GameObject.FindGameObjectWithTag("Player");
    //player 태그를 가진 오브젝트를 찾아옴
    if(player != null)    //플레이어가 존재하면
    {
        float d = Vector2.Distance(transform.position, player.transform.position);
        //플레이어와 gimmickblock의 거리 계산
        if(length >= d)    //거리가 length랑 같거나 작으면 bodyType을 Dynamic으로 변경
        {
            rbody.bodyType = RigidbodyType2D.Dynamic;
            //Rigidbody2D rbody = GetComponent<Rigidbody2D>();
            //if(rbody.bodyType == RigidbodyType2D.Static)
            //{ 
            //    rbody.bodyType = RigidbodyType2D.Dynamic;
            //} if문이 필요한가??? 어차피 떨어지면 끝이자너,,,,
        }
    }
    if(isFell)    //낙하 플래그 True(가 되려면 충돌 함수에서 isDelete 조건을 먼저 통과해야함)
    {
        fadeTime -= Time.deltaTime;    //이전 프레임과의 차이만큼 시간 차감
        Color col = GetComponent<SpriteRenderer>().color;    //현재 색
        col.a = fadeTime;    //투명도 변경
        GetComponent<SpriteRenderer>().color = col;    //색 재설정
        if(fadeTime <= 0.0f)    //fadeTime 0에 도달하면 gimmick block 제거
        {
            Destroy(gameObject);
        }
    }
}

if(isFell) : isDelete 활성화 + 충돌이 전제되어야 실행 가능

Color : r, g, b, a의 값 존재
color.a(불투명도) : 0 ~ 1
color.a = fadeTime : 경과 시간에 따라 점점 0에 가까워짐

* fadeTime = fadeTime - Time.deltaTime;

 


OnCollisionEnter2D(Collision2D collision)

void OnCollisionEnter2D(Collision2D collision)    //gimmick block에 충돌 감지
{
    //isDelete 활성화가 되어있으면 isFell true로 변경하여 Update()에서 투명도 설정 가능
    if(isDelete)
    {
        isFell = true;
    }
}

 

 

 

*교재 : 누구나 할 수 있는 유니티 2D 게임 제작