티스토리 뷰

Sword 스킬을 구현할 때 따라가기에 바빠서 수업중엔 제대로 이해하지 못했다.. 다시 여기에 정리하면서 이해했다…


💻 Unity

Netcode

Unity에서 멀티플레이어 게임을 개발할 때 사용하는 네트워킹 솔루션으로 로컬에서 동작하는 서버-클라이언트 구조를 쉽게 구현할 수 있게 한다

NetworkManager 서버와 클라이언트, 연결 관리
NetworkObject 네트워크에서 동기화되는 오브젝트
NetworkBehaviour 네트워크 동작을 담당하는 스크립트
ServerRPC 클라이언트 → 서버 호출 함수
ClientRPC 서버 → 클라이언트 호출 함수
Ownership 오브젝트의 주인(Owner) 개념

🕹️ 실습 (Netcode)

[RequireComponent(typeof(Button))]
public class StartGame : MonoBehaviour
{
    private Button _button;
    
    void Start()
    {
        _button = GetComponent<Button>();
        _button.onClick.AddListener(StartGameProcess);
    }

    void StartGameProcess()
    {
        NetworkManager.Singleton.SceneManager.LoadScene("Playground", LoadSceneMode.Single);
    }
}

🕹️ 실습 (2D 횡스크롤 스테이트머신)

Sword 꽂히는 매커니즘

// SwordSkillController.cs

private void OnTriggerEnter2D(Collider2D collision) // 충돌체와 부딪혔을 
{
    if (isReturni
	{
		return;
	}

    collision.GetComponent<Enemy>()?.TakeDamage();

    StuckInto(collision); // 꽂히기 처리
}

private void StuckInto(Collider2D collision)
{
    if (pierceAmount > 0 && collision.GetComponent<Enemy>() != null)
    {
        pierceAmount--; // Pierce 타입일 경우 관통 처리
        return;
    }

    // 검 고정 설정
    canRotate = false;
    cd.enabled = false; // 콜라이더 비활성화

    // 물리 설정 변경
    rb.bodyType = RigidbodyType2D.Kinematic;
    rb.constraints = RigidbodyConstraints2D.FreezeAll;

    anim.SetBool("Rotation", false); // 회전 애니메이션 중지
    transform.parent = collision.transform; // 충돌체에 고정
}

Sword 돌아오는 매커니즘

// SwordSkillController.cs

private void Update()
{

    if (isReturning)
    {
		// 플레이어에게 이동
        transform.position = Vector3.MoveTowards(transform.position, 
                                              player.transform.position, 
                                              returnSpeed * Time.deltaTime);
        
        // 플레이어와 가까워지면 검 제거
        if (Vector2.Distance(transform.position, player.transform.position) < 1)
        {
            player.ClearSword();
        }
    }
}

public void ReturnSword()
{
    rb.bodyType = RigidbodyType2D.Dynamic; // 물리 활성화
    rb.constraints = RigidbodyConstraints2D.FreezeAll;
    transform.parent = null; // 부모 해제
    isReturning = true; // 반환 상태 설정
}
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함