[멋쟁이사자처럼 부트캠프 TIL] 멋쟁이사자처럼유니티게임스쿨 3일차

굥지·2024년 9월 30일
0

타일맵 만들기

사용 에셋

Pixel Adventure 1

Free - Adventure Pack - Grassland

TileMap 종류

rectangular : 사각형(기본)
hexagonal : 육각형
isometic : 2.5D? 메이플2 같은 뷰


▲ hexagonal

▲ isometic

TileMap 설정하기

가져온 타일맵의 pixel per unit을 25로 하고 grid크기를 0.64(16/25)로 해준다


pixel per unit을 16으로하고 grid크기를 1,1로 하는거랑 뭐가 다른지?

→ 한 영역에 담기는 그리드 개수가 달라짐. 25, 0.64로하면 더 많은 그리드가 생김

콜라이더 설정

만든 Tile Map이 생성된 자식 오브젝트 이름을 Terrain로 설정한 후, composite collider 컴포넌트(+rigd가 동시에 생김)를 달아준다.

composite collider
Tilemap Collider를 넣었을때 타일 하나하나가 Collider 적용이 되기 때문에 composite collider로 하나의 콜라이더로 통합해준다.

타일 맵의 땅이 떨어지는걸 막기 위해 static으로 바꿔줌

Player도 rigd와 collider추가

Player

GetAxis : 부드러운 중간값도 출력
GetAxisRaw : 키를 누르자마자 반응

Player 이동

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float Speed = 5;
    float vx = 0;
    void Start()
    {
        
    }

    void Update()
    {
        vx = Input.GetAxisRaw("Horizontal")*Speed;
        GetComponent<Rigidbody2D>().linearVelocity = new Vector2(vx,0);
    }
}

linearVelocity랑 velocity랑 같은 것

버전이 바뀌면서 velocity → linearVelocity로 바뀜

Player를 바라보는 방향으로 보게하기

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float Speed = 5;
    float vx = 0;
    void Start()
    {
        
    }

    void Update()
    {
        vx = Input.GetAxisRaw("Horizontal")*Speed;

        if(vx<0)
        {
            GetComponent<SpriteRenderer>().flipX = true;
        }
        if (vx > 0)
        {
            GetComponent<SpriteRenderer>().flipX = false;
        }

        GetComponent<Rigidbody2D>().linearVelocity = new Vector2(vx,0);
    }
}

Player 점프 구현

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float Speed = 5;
    public float JumpSpeed = 5;

    float vx = 0;

    void Start()
    {
        
    }

    void Update()
    {
        vx = Input.GetAxisRaw("Horizontal")*Speed;
        float vy = GetComponent<Rigidbody2D>().linearVelocityY;

        if(vx<0)
        {
            GetComponent<SpriteRenderer>().flipX = true;
        }
        if (vx > 0)
        {
            GetComponent<SpriteRenderer>().flipX = false;
        }
        if(Input.GetButtonDown("Jump"))
        {
            vy = JumpSpeed;
        }

        GetComponent<Rigidbody2D>().linearVelocity = new Vector2(vx,vy);
    }
}

float vy = GetComponent<Rigidbody2D>().linearVelocityY;
위 Y 값을 주고 GetComponent<Rigidbody2D>().linearVelocity = new Vector2(vx,vy); 로 수정해준다

Discrete : 빠른 계산
Continuous : 정밀한 계산

Player가 벽타는 현상(마찰력 줄이기)


PhysicsMaterial 컴포넌트를 만들고 Player의 Rigidbody에 붙여주면 벽에 붙는 현상이 없어진다.

Discrete로 설정하면 높이 뛰었다가 내려올때 collider를 뚫고 내려갈 수 있기 때문에 Continuous로 바꿔준다

무한 점프 방지

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float Speed = 5;
    public float JumpSpeed = 5;
    public Collider2D BottomCollider;
    public CompositeCollider2D TerrainCollder;

    float vx = 0;

    void Start()
    {
        
    }

    void Update()
    {
        if(BottomCollider.IsTouching(TerrainCollder))
        {
            Debug.Log("Grounded");
        }
        else
        {
            Debug.Log("NOT");
        }

        vx = Input.GetAxisRaw("Horizontal")*Speed;
        float vy = GetComponent<Rigidbody2D>().linearVelocityY;

        if(vx<0)
        {
            GetComponent<SpriteRenderer>().flipX = true;
        }
        if (vx > 0)
        {
            GetComponent<SpriteRenderer>().flipX = false;
        }
        if(Input.GetButtonDown("Jump"))
        {
            vy = JumpSpeed;
        }

        GetComponent<Rigidbody2D>().linearVelocity = new Vector2(vx,vy);
    }

}

public Collider2D BottomCollider; public CompositeCollider2D TerrainCollder;

콜라이더를 선언하고 넣어준 후, if(BottomCollider.IsTouching(TerrainCollder))

IsTouching : 2D 물리 시스템에서 Collider2D 컴포넌트 간의 충돌 여부를 확인할 때 사용
나의 콜라이더.IsTouching(닿을 콜라이더)
로 사용한다.


        if (BottomCollider.IsTouching(TerrainCollder))
        {
            isGround = true;
        }
        else
        {
            isGround = false;
        }

이렇게 한줄로 요약 가능

isGround = BottomCollider.IsTouching(TerrainCollder);
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float Speed = 5;
    public float JumpSpeed = 5;
    public Collider2D BottomCollider;
    public CompositeCollider2D TerrainCollder;

    float vx = 0;
    bool isGround;

    void Start()
    {
        
    }

    void Update()
    {
        vx = Input.GetAxisRaw("Horizontal")*Speed;
        float vy = GetComponent<Rigidbody2D>().linearVelocityY;

        if(vx<0)
        {
            GetComponent<SpriteRenderer>().flipX = true;
        }
        if (vx > 0)
        {
            GetComponent<SpriteRenderer>().flipX = false;
        }

        isGround = BottomCollider.IsTouching(TerrainCollder);

        if (Input.GetButtonDown("Jump") && isGround == true)
        {
            vy = JumpSpeed;
        }

        GetComponent<Rigidbody2D>().linearVelocity = new Vector2(vx,vy);
    }

}

카메라가 따라오도록 하기

시네머신 카메라의 활용 : 카트라이더에 뒤에서 비추는 카메라 구현도 가능

Unity Registry에서 시네머신 import
cinemachine camera 추가 후, follow 컴포넌트와 target을 player로 지정하면 따라오게 한다

Border(화면 밖으로 나가지 못하게하는 경계선)을 collider로 만들고, 시네마 카메라에 confiner 2D를 추가한 후 Border을 할당해준다

BoxCollider은 꼭짓점 정보를 안 줌 그래서 Edge Collider로 사용

🚨 보더 크기가 카메라보다 훨씬 커야 오류가 안 뜸(작으면 플레이어를 안 따라옴)

Player 애니메이션


2D에는 다 0,0 으로 세팅

다른 애들도 다 Can Transition To Self를 켜준다

using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float Speed = 5;
    public float JumpSpeed = 5;
    public Collider2D BottomCollider;
    public CompositeCollider2D TerrainCollder;

    float prevVx = 0;
    float vx = 0;
    bool isGround;
    

    void Start()
    {
        
    }

    void Update()
    {
        vx = Input.GetAxisRaw("Horizontal")*Speed;
        float vy = GetComponent<Rigidbody2D>().linearVelocityY;

        if(vx<0)
        {
            GetComponent<SpriteRenderer>().flipX = true;
        }
        if (vx > 0)
        {
            GetComponent<SpriteRenderer>().flipX = false;
        }

        if (BottomCollider.IsTouching(TerrainCollder))
        {
            if (!isGround)
            {
                if(vx == 0) //속도가 0일때, 가만히 있는다면
                {
                    GetComponent<Animator>().SetTrigger("Idle");
                }
                else
                {
                    GetComponent<Animator>().SetTrigger("Run");
                }
            }
            else
            {
                if (vx != prevVx)
                {
                    if (vx == 0)
                    {
                        GetComponent<Animator>().SetTrigger("Idle");
                    }
                    else
                    {
                        GetComponent<Animator>().SetTrigger("Run");
                    }
                }
            }
        
        }
        else
        {
            if(isGround)
            {
                GetComponent<Animator>().SetTrigger("Jump");
            }
        }

        isGround = BottomCollider.IsTouching(TerrainCollder); //콜라이더들이 부딪히면 true를 isGround에 

        if (Input.GetButtonDown("Jump") && isGround == true)
        {
            vy = JumpSpeed;
        }

        GetComponent<Rigidbody2D>().linearVelocity = new Vector2(vx,vy);
    }

}

각 조건마다 애니메이션을 다르게 실행하도록 해준다

0개의 댓글

관련 채용 정보