1. 지면 & 백그라운드 제작
캐릭터 컴포넌트의 제작이 어느정도 끝났으니 스테이지를 만들기로했다.
지면이랑 장애물을 어떻게 만들지 고민했는데 지면은 그냥 스크롤링으로 하고 장애물만 부착하면 시간을 단축시킬 수 있을 것 같았다. 그래서 그라운드 스크롤링 게임오브젝트를 만들고 그라운드 스크롤 스크립트를 부착해서 일단 캐릭터와 지면 사이의 거리를 디버그로 찍어 보았다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GroundScrolling : MonoBehaviour
{
[SerializeField]
private GameObject ch;
[SerializeField]
private GameObject gr;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Debug.Log(ch.transform.position.x - gr.transform.position.x);
}
}
그 다음엔 그라운드는 일정값을 벗어났을때 위치를 초기화 하도록 해주고 백그라운드는 캔버스에 넣어서 메인화면에 썼던것처럼 위치를 초기화 시켜주기로 하였다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
public class GroundScrolling : MonoBehaviour
{
[SerializeField]
private Transform ch;
[SerializeField]
private Transform gr;
[SerializeField]
private Transform bg;
private float bgSpeed =0.5f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(ch.position.x - gr.position.x > 8f)
{
gr.position = new Vector3(gr.position.x + 15.5f, gr.position.y, gr.position.z);
}
if (this.transform.position.x < -1024)
{
this.transform.position = this.transform.position + new Vector3(1024, 0, 0);
}
bg.position = new Vector3(bg.position.x - bgSpeed * Time.deltaTime, bg.position.y, bg.position.z);
}
}
기본적인 베이스는 완성!
2. 구조물 제작
이번에는 캐릭터가 밟고 지나갈 수 있는 발판과 장애물을 설계하도록 하겠다. 장애물의 경우 삼각형으로 되어 있는데 캐릭터가 접촉할 경우 파괴된다. 찾아보니까 삼각형을 구현하는데는 Edge Collider2d를 사용하면 된다고 한다. 에셋은 http://vignette1.wikia.nocookie.net/geometry-dash/images/8/8a/RegularSpike01.png에서 다운받아왔다.
일단 붙이긴 했는데 엣지콜라이더는 처음 붙여 보는거라 좀 긴가민가하다 그래서 obstacle 태그를 만들어서 장애물에 붙여 주고 Debug를 한번 찍어 보았다.
private void OnCollisionEnter2D(Collision2D collision)
{
if(collision.collider.tag == "ground")
{
isGround = true;
this.rb.velocity = Vector2.zero;
this.rb.angularVelocity = 0f;
}
else if(collision.collider.tag == "obstacle")
{
Debug.Log("장애물과 접촉");
}
}
잘 작동하는 모습 나머지 구조물도 만들어서 콜라이더를 부착해주었다.
그리고 지오메트리대쉬는 지면의 옆면에 닿아도 파괴되므로 레이를 쏴서 지면의 옆면에 닿을 경우 게임 오브젝트를 비활성화 하도록 만들어 봤다
Vector2 chRay = new Vector2(this.transform.position.x, (this.transform.position.y - (GetComponent<BoxCollider2D>().size.y / 4)));
Debug.DrawRay(chRay, Vector2.right* (GetComponent<BoxCollider2D>().size.x / 2.8f),Color.red,0.5f);
if(Physics2D.Raycast(chRay , Vector2.right, (GetComponent<BoxCollider2D>().size.x / 2.8f),1<<6)){
this.gameObject.SetActive(false);
}
이 다음부턴 박자에 맞게 장애물과 발판들을 등장시켜서 지오메트리 대시의 스테이지를 진짜로 만들어보도록 할 것이다.
'2D 콘텐츠 제작' 카테고리의 다른 글
Geometry Dash [#5] - 시네머신 / 우주선 만들기 (0) | 2023.09.19 |
---|---|
Geometry Dash [#4] - 스테이지 장애물 발판 설치 (0) | 2023.09.18 |
Geometry Dash [#2] - 씬전환 / 캐릭터 (0) | 2023.09.14 |
Geometry Dash [#1] - 게임시작화면 만들고 배경 스크롤링 (0) | 2023.09.11 |
Geometry Dash [#0] - 게임분석 (0) | 2023.09.07 |