[내일 배움 캠프 Unity 4기] W5D5 05.17 TIL

김용준·2024년 5월 17일
0

내일배움캠프

목록 보기
16/47

Goals

  • Develop the frame of single play scene
  • Implement collision system

Single play scene of team project

My tasks for the team project is to build scene with the logic for the single player of avoid shit. The main body of the game is falling shits. To generate the repeating object, I have designed the script like below.

public class GameManager : MonoBehaviour
{
  public GameObject[] fallingObjs;
  
  public void Start()
  {
    InvokeRepeating("GenerateObj",0f,1f);
  }
  
  private void GenerateObj()
  {
    foreach(var obj in fallingObjs)
      Instantiate(obj);
  }
}

From the lecture on the couple of weeks ago, I learned the generation system of repeating. This method could be utilized to the current team project.

Since my role is to design the rule of single play, there is the one who make animation and assets. So I made the rule with the simple sprites as shown in following figure.


Implement collision system

The collision between player and falling object should be defined. With the RigidBody2D and *Collider2D, I have designed the C# script of falling obj like this.

   private void OnCollisionEnter2D(Collision2D coll)
   {
       if((coll.gameObject.CompareTag("Ground") || 
           coll.gameObject.CompareTag("Rock")) &&
           !gameObject.CompareTag("Rock"))
       {
           if (opt == 1) // In case of meteor, the meteor will be destroyed
               Destroy(gameObject);
           rFE.SetActive(false);
       }
       else if (coll.gameObject.CompareTag("Player"))
       {
           foreach(var i in coll.contacts)
           {
               Debug.Log(i.normal);
           }
           if(coll.contacts[0].normal.y == -1) // 플레이어가 밟았을때
           {
               count++; // 멀쩡한 바위 -> 금간 바위
               if(count == 1) // 금간 바위 -> 터짐
                   Destroy(gameObject);
           }
           else if (coll.contacts[0].normal.y == 1) // 플레이어 머리위로 떨어졌을때
           { 
               // 플레이어 체력감소 || 게임 오버
               GameManager.instance.GameOver();
           }
       }
   }

The new method that I tried in this script is the contacts. Attaching by the Collition2D class, it gaves the array of ContactPosition2D. Facilitating this type, we can extract the normal vector by coll.contacts[0].normal. Because the collision occurs with the line on the collider geometry, there are two collision points on single collision. By testing with the difference on the falling obj, this method gives the edge of collided line. Classifying the events on the normal axis, I could design the scripts.

profile
꿈이큰개발자지망생

0개의 댓글