[Unity] FlappyPlane - 하위 컴포넌트 가져오기, Collider 충돌의 차이, LocalPosition

Gee·2025년 2월 14일

GetComponentInChildren

    void Start()
    {
        animator = GetComponent<Animator>();  		// Animator 컴포넌트를 가져옴
        _rigidbody = GetComponent<Rigidbody2D>();   // Rigidbody2D 컴포넌트를 가져옴
    }
  • Player의 움직임 구현을 위해 Animator 컴포넌트와 Rigidbody2D를 불러오는 코드를 작성.
  • Rigidbody2D는 Player의 컴포넌트에 붙어있기 때문에 GetComponent로 잘 호출됨.
  • 하지만 Animator는 Player의 하위 오브젝트인 Model에 달려 있음. 이런 형태로는 부를 수 없음
    void Start()
    {
        animator = GetComponentInChildren<Animator>();  // Animator 컴포넌트를 가져옴
        _rigidbody = GetComponent<Rigidbody2D>();       // Rigidbody2D 컴포넌트를 가져옴
    }
  • 그래서 GetComponent 뒤에 InChildren을 붙여주면, 내 오브젝트에서만 찾는 GetComponent와 달리 하위 오브젝트(하이어라키 창의 자식 오브젝트들)까지 검색을 진행한다.
  • 그래서 Player 하위의 Model Animator를 찾아올 수 있게 된다.

Collider: Collision과 Trigger의 차이

  • Collider는 두 가지 형태로 충돌함. Collision과 Trigger.
  • Collision 충돌: 실제 물리적인 힘이 가해지면서 충돌.
  • Trigger: 물리적인 충돌에 대한 통보만 해 주고 실제로 물리적 충돌은 하지 않음.

LocalPosition과 Position의 차이

    public Vector3 setRandomPlace(Vector3 lastPosition, int obstacleCount)
    {
        float holeSize = Random.Range(holeSizeMin, holeSizeMax);
        float halfHoleSize = holeSize / 2;

        // holeSize만큼 두 오브젝트를 벌려주기
        topObject.localPosition = new Vector3(0, halfHoleSize);     // holesize가 나온 거에 반만큼을 위로 올림
        bottomObject.localPosition = new Vector3(0, -halfHoleSize); // holesize가 나온 거에 반만큼을 아래로 내림

        // 제일 마지막에 놓인 오브젝트 뒤에다가 widthPadding만큼 더한 값으로 이동시킴. (마지막에 배치된 오브젝트 뒤에 배치하는 것)
        Vector3 placePosition = lastPosition + new Vector3(widthPadding, 0);
        placePosition.y = Random.Range(lowPosY, highPosY);      // 랜덤으로 위치를 구함

        transform.position = placePosition;

        return placePosition;
    }
  • 각각 로컬 좌표와 월드 좌표를 기준으로 하는 것.
  • Transform.Position은 월드에서 오브젝트의 절대 좌표값을 나타낸다(원점 0, 0, 0으로부터의 위치).
  • 기본적으로는 포지션으로 월드 좌표를 사용하고 있음.
  • Transform.LocalPosition은 부모 오브젝트에 대한 자식 오브젝트의 상대적 위치를 나타낸다.
  • 로컬 포지션은 부모를 기준으로 이동, 회전, 크기가 계산됨.
  • 월드 중심을 기준으로 할 건지 부모 오브젝트를 기준으로 할 건지의 차이.
profile
...

0개의 댓글