๐Ÿ–ฅ Unity2D Project : ์ปดํ“จํ„ฐ ์‚ด๋ฆฌ๊ธฐ - ์บ๋ฆญํ„ฐ ์ปจํŠธ๋กค

Se0ng_1lยท2023๋…„ 5์›” 7์ผ

์ปดํ“จํ„ฐ ์‚ด๋ฆฌ๊ธฐ

๋ชฉ๋ก ๋ณด๊ธฐ
2/8

์กฐ์ž‘์œผ๋กœ ์บ๋ฆญํ„ฐ๋ฅผ ์›€์ง์—ฌ๋ณด์ž

๐Ÿ“บ๋ฏธ๋ฆฌ๋ณด๊ธฐ

1๏ธโƒฃ. ์บ๋ฆญํ„ฐ ๋””์ž์ธ - Aseprite ํ”„๋กœ๊ทธ๋žจ

2๏ธโƒฃ. ์ ๋‹นํžˆ ์Šคํ”„๋ผ์ดํŠธ๋ฅผ ์ž˜๋ผ์ฃผ์ž


3๏ธโƒฃ. ํ”Œ๋ ˆ์ด์–ด ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์ž‘์„ฑํ•˜์ž

โ€‚โ€‚โ€‚โ€‚1. Collider์™€ ๋ฆฌ์ง€๋“œ๋ฐ”๋””๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ์›ํ•˜๋Š” ์„ค์ •๊ฐ’์„ ๋„ฃ๋Š”๋‹ค.

1. ๋งŒ๋“ค ๊ฒŒ์ž„์€ ์ค‘๋ ฅ์ด ์ž‘์šฉํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ RigidBody์—์„œ ์ค‘๋ ฅ์„ 0์œผ๋กœ ์„ค์ •
2. ์ ๋‹นํ•œ ์‚ฌ์ด์ฆˆ๋กœ ์ฝœ๋ผ์ด๋” ํฌ๊ธฐ ์กฐ์ ˆ

โ€‚โ€‚โ€‚โ€‚2. ์ž…๋ ฅ์— ๋”ฐ๋ฅธ ์ด๋™ ์Šคํฌ๋ฆฝํŠธ

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    [SerializeField] private float speed;
    private Vector2 inputVec;

    private Rigidbody2D rigid;
    private Collider2D col;

    private void Awake()
    {
        rigid = GetComponent<Rigidbody2D>();
        col = GetComponent<Collider2D>();
    }

    private void Update()
    {
        inputVec.x = Input.GetAxisRaw("Horizontal");
        inputVec.y = Input.GetAxisRaw("Vertical");
    }

    private void FixedUpdate()
    {
        Vector2 nextVec = inputVec * speed * Time.fixedDeltaTime;
        rigid.MovePosition(rigid.position + nextVec);
    }
}

โ€‚โ€‚โ€‚โ€‚3. ์ด๋™ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ์Šคํ”„๋ผ์ดํŠธ ๋ฐ˜์ „

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    [SerializeField] private float speed;
    private Vector2 inputVec;

    private Rigidbody2D rigid;
    private Collider2D col;
    private SpriteRenderer spriter;

    private void Awake()
    {
        rigid = GetComponent<Rigidbody2D>();
        col = GetComponent<Collider2D>();
        spriter = GetComponent<SpriteRenderer>();
    }

    private void Update()
    {
        inputVec.x = Input.GetAxisRaw("Horizontal");
        inputVec.y = Input.GetAxisRaw("Vertical");
    }

    private void FixedUpdate()
    {
        Vector2 nextVec = inputVec * speed * Time.fixedDeltaTime;
        rigid.MovePosition(rigid.position + nextVec);
    }

    private void LateUpdate()
    {
        if (inputVec.x != 0)
        {
            spriter.flipX = inputVec.x > 0;
        }
    }
}

โ€‚โ€‚โ€‚โ€‚4. ์• ๋‹ˆ๋ฉ”์ด์…˜ ์ถ”๊ฐ€

์‚ฌ์ „ ์ž‘์—…์„ ์•„๋ž˜ ์‚ฌ์ง„๊ณผ ๊ฐ™์ด ์กฐ์ ˆํ•œ๋‹ค.

์ตœ์ข… ์ฝ”๋“œ

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
    [SerializeField] private float speed;
    private Vector2 inputVec;

    private Rigidbody2D rigid;
    private Collider2D col;
    private SpriteRenderer spriter;
    private Animator anim;

    private void Awake()
    {
        rigid = GetComponent<Rigidbody2D>();
        col = GetComponent<Collider2D>();
        spriter = GetComponent<SpriteRenderer>();
        anim = GetComponent<Animator>();
    }

    private void Update()
    {
        inputVec.x = Input.GetAxisRaw("Horizontal");
        inputVec.y = Input.GetAxisRaw("Vertical");
    }

    private void FixedUpdate()
    {
        Vector2 nextVec = inputVec * speed * Time.fixedDeltaTime;
        rigid.MovePosition(rigid.position + nextVec);
    }

    private void LateUpdate()
    {
        // ๋‹จ์ˆœ Vector์˜ ํฌ๊ธฐ๋กœ ์• ๋‹ˆ๋ฉ”์ด์…˜์— ์ ์šฉํ•˜๊ธฐ ์œ„ํ•ด magnitude๋ฅผ ์‚ฌ์šฉ
        anim.SetFloat("Speed", inputVec.magnitude); 
        if (inputVec.x != 0)
        {
            spriter.flipX = inputVec.x > 0;
        }
    }
}

โญ๏ธ ๊ฒฐ๊ณผ

์ฐธ๊ณ ์ž๋ฃŒ

๊ณจ๋“œ๋ฉ”ํƒˆ ์œ ํŠœ๋ธŒ - ์œ ๋‹ˆํ‹ฐ ๋ฑ€์„œ๋ผ์ดํฌ

https://youtu.be/MmW166cHj54

profile
์น˜ํƒ€๊ฐ€ ๋˜๊ณ  ์‹ถ์€ ์ทจ์ค€์ƒ

0๊ฐœ์˜ ๋Œ“๊ธ€