2 minute read

1. ๊ธฐ๋ณธ ํ™”๋ฉด ๊ตฌ์„ฑํ•˜๊ธฐ

์ด์ „๊ณผ ๊ฐ™์ด Bounce Ball์ด๋ผ๋Š” ํ˜•์‹์œผ๋กœ ๊ฒŒ์ž„์„ ๋งŒ๋“ ๋‹ค.
๋Œ€์‹  ์ด๋ฒˆ์—๋Š” 2D๋กœ ๊ฒŒ์ž„ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

์ดํ›„ 2D sprite๋ฅผ ์ƒ์„ฑํ›„ ์ž์‹ ์ด ์›ํ•˜๋Š” Asset์„ ๋„ฃ๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ธฐ๋ณธ์ ์ธ ๊ณต๊ณผ ๋•…์„ ๊ตฌํ˜„ํ•ด์ค€๋‹ค.

์Šคํฌ๋ฆฐ์ƒท(14)

์ด๋•Œ ํ•˜๋‚˜์˜ ๋•…์˜ ํฌ๊ธฐ๋ฅผ (1, 1)๋กœ ์ง€์ •ํ•˜์—ฌ ์—ฌ๋Ÿฌ๊ฐœ์˜ ๋ธ”๋Ÿญ์œผ๋กœ ๋•…์„ ๋ฐฐ์น˜ํ•˜๊ธฐ๋กœ ํ•˜์˜€๋‹ค.

image

์ดํ›„ ๋•…์—๋Š” Box Collider 2D๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๊ณ  ๊ณต์—๋Š” Rigidbody 2D์™€ Circle Collider 2D๋ฅผ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.
๋˜ํ•œ Player์™€ Floorํƒœ๊ทธ๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋„ฃ์–ด์ค€๋‹ค.


2. Jump์™€ ์ด๋™ ๊ตฌํ˜„ํ•˜๊ธฐ


์ ํ”„์™€ ํ”Œ๋ ˆ์ด์–ด ์ด๋™์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ์ƒˆ Bounce_Player๋ผ๋Š” Script๋ฅผ ๋งŒ๋“ ํ›„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

Rigidbody2D rigid;
bool _isJump;
public float JumpPower;
public float limit_speed;
void Start()
{
    limit_speed = 5;
    JumpPower = 3;
    _isJump = true;
    rigid = GetComponent<Rigidbody2D>();
}
void Update()
{
    PlayerMove();
    StartCoroutine(Jump());
}
void PlayerMove()
{
    float h = Input.GetAxis("Horizontal");
    rigid.AddForce(Vector2.right * 10 * h * Time.deltaTime, ForceMode2D.Impulse);

    if (rigid.velocity.x > limit_speed)
    {
        rigid.velocity = new Vector2(limit_speed, rigid.velocity.y);
    }
    else if (rigid.velocity.x < -limit_speed)
    {
        rigid.velocity = new Vector2(-limit_speed, rigid.velocity.y);
    }

}
IEnumerator Jump()
{
    if (_isJump == false)
    {
        rigid.AddForce(Vector2.up * JumpPower, ForceMode2D.Impulse);
        print("jump");
        _isJump = true;
        yield return null;
    }
}
private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.collider.CompareTag("Floor")){
        _isJump = false;
        rigid.velocity = rigid.velocity / 2;
    }
}

ํ•˜์ง€๋งŒ ์ด ์ฝ”๋“œ์—๋Š” ํ•œ๊ฐ€์ง€ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค. ๋ฐ”๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์˜†์— ๋ฐฐ์น˜๋œ ๋ฐ”๋‹ฅ์— ๋‹ฟ๊ธฐ๋งŒ ํ•ด๋„ ์ ํ”„๊ฐ€ ๋œ๋‹ค.

bounce problem

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•˜์—ฌ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฏธ์„ธํ•˜๊ฒŒ FloorTop์ด๋ผ๋Š” Sprite๋ฅผ ๋งŒ๋“ค๊ณ  ํˆฌ๋ช…๋„๋ฅผ 0์œผ๋กœ ๋ฐ”๊พผํ›„ OnCollisionEnter2D์ฝ”๋“œ์˜ CompareTag๋ฅผ FloorTop์œผ๋กœ ๋ฐ”๊พธ์–ด ์ฃผ์—ˆ๋‹ค.

์Šคํฌ๋ฆฐ์ƒท(13)


3. Camera ์ด๋™

๋งต์„ ์ด๋™ํ•˜๋Š”๋ฐ ์นด๋ฉ”๋ผ๊ฐ€ ์ •์ง€๋˜์–ด ์žˆ์œผ๋ฉด ๋งต ์‚ฌ์ด์ฆˆ์— ์ œ์•ฝ์„ ๋ฐ›์•„์„œ ์นด๋ฉ”๋ผ ๋ฌด๋ธŒ ๊ด€๋ จ๋˜์–ด Script๋ฅผ ๋งŒ๋“ค์–ด ์ค€๋‹ค.
์ƒˆ Bounce Camera Script๋ฅผ ๋งŒ๋“ ํ›„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž‘์„ฑํ•ด์ค€๋‹ค.


public GameObject player;
Vector3 target;
Vector3 velo;
void Start()
{
    velo = Vector2.zero;
    StartCoroutine(CameraMove());
}
IEnumerator CameraMove()
{
    while (true)
    {
        target = player.GetComponent<Transform>().position + Vector3.back * 10;
        transform.position = Vector3.SmoothDamp(transform.position, target, ref velo, 1f);
        yield return null;
    }
}


bounce Camera


4. ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์ƒํ˜ธ์ž‘์šฉ ์˜ค๋ธŒ์ ํŠธ ๋งŒ๋“ค๊ธฐ


๊ฒŒ์ž„์—์„œ ์‚ฌ์šฉํ•  ์—ฌ๋Ÿฌ๊ฐ€์ง€ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์—ˆ๋‹ค.

image

์ด์ค‘ WeakFloor์™€ Jump์—๋„ ์ถ”๊ฐ€์ ์œผ๋กœ Top์„ ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ๋‹ค.
๊ทธ๋ฆฌ๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์—ฌ๋Ÿฌ ์˜ค๋ธŒ์ ํŠธ๊ฐ„์˜ ์ƒํ˜ธ์ž‘์šฉ์„ ์ถ”๊ฐ€ํ•ด ์ฃผ์—ˆ๋‹ค.


Rigidbody2D rigid;
bool _isJump;
public float JumpPower;
public float Instance_JumpPower;
public float limit_speed;
public Vector2 Start_Point;

void Start()
{
    limit_speed = 5;
    JumpPower = 3;
    Instance_JumpPower = JumpPower;
    _isJump = true;
    rigid = GetComponent<Rigidbody2D>();

    Start_Point = new Vector2(0, 5);
}

IEnumerator Jump()
{
    if (_isJump == false)
    {
        rigid.AddForce(Vector2.up * Instance_JumpPower, ForceMode2D.Impulse);
        _isJump = true;
        Instance_JumpPower = JumpPower;
        yield return null;
    }
    if (transform.position.y < -5)
    {
        Die();
    }
}

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.collider.CompareTag("FloorTop"))
    {
        _isJump = false;
        rigid.velocity = rigid.velocity / 2;
    }
    else if (collision.collider.CompareTag("WeakFloorTop"))
    {
        _isJump = false;
        rigid.velocity = rigid.velocity / 2;
    }
    else if (collision.collider.CompareTag("JumpTop"))
    {
        _isJump = false;
        rigid.velocity = rigid.velocity / 2;
        Instance_JumpPower = JumpPower * 1.5f;
    }
    else if (collision.collider.CompareTag("ThornTop") || collision.collider.CompareTag("Monster"))
    {
        Death();
    }
    else if (collision.collider.CompareTag("Coin"))
    {
        
    }
    else if (collision.collider.CompareTag("Portal"))
    {
        Clear();
    }
}
void Death()
{
    rigid.velocity = rigid.velocity * 0;
    transform.position = Start_Point;
}

void Clear()
{

}


3

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ž˜ ์ž‘๋™๋˜๋Š” ๋ชจ์Šต์ด๋‹ค.
์ง€๊ธˆ๊นŒ์ง€๋Š” ์˜ค๋ธŒ์ ํŠธ์— ๋‹ฟ์œผ๋ฉด ํ”Œ๋ ˆ์ด์–ด์˜ ์ƒํƒœ์— ๋Œ€ํ•ด์„œ ์กฐ์ ˆํ•˜์˜€๋‹ค.
๋‹ค์Œ์€ ์˜ค๋ธŒ์ ํŠธ๊ฐ€ ์‚ฌ๋ผ์ง€๊ฑฐ๋‚˜ ์ด๋™ํ•˜๋Š” ๋“ฑ๋„ ๊ตฌํ˜„ํ•  ๊ฒƒ์ด๋‹ค.



๊ฐœ์ธ ๊ณต๋ถ€ ๊ธฐ๋ก์šฉ ๋ธ”๋กœ๊ทธ์ž…๋‹ˆ๋‹ค.
ํ‹€๋ฆฌ๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์„ ๊ฒฝ์šฐ ์ œ๋ณดํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.๐Ÿ˜