mooni
[Unity] 3차 Study : GimmickBlock Script 본문
[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 게임 제작
'Unity' 카테고리의 다른 글
[Unity] 3차 Study : SwitchAction Script (0) | 2024.03.12 |
---|---|
[Unity] 3차 Study : MovingBlock Script (0) | 2024.03.12 |
[Unity] 3차 Study : PalyerController Script (0) | 2024.03.11 |
[Unity] Chap05. 버튼과 UI 만들기 (0) | 2024.03.07 |
[Unity] Chap04. 사이드뷰 게임의 기본 시스템 만들기 (1) | 2024.03.07 |