티스토리 뷰
Sword 스킬을 구현할 때 따라가기에 바빠서 수업중엔 제대로 이해하지 못했다.. 다시 여기에 정리하면서 이해했다…
💻 Unity
Netcode
Unity에서 멀티플레이어 게임을 개발할 때 사용하는 네트워킹 솔루션으로 로컬에서 동작하는 서버-클라이언트 구조를 쉽게 구현할 수 있게 한다
NetworkManager | 서버와 클라이언트, 연결 관리 |
NetworkObject | 네트워크에서 동기화되는 오브젝트 |
NetworkBehaviour | 네트워크 동작을 담당하는 스크립트 |
ServerRPC | 클라이언트 → 서버 호출 함수 |
ClientRPC | 서버 → 클라이언트 호출 함수 |
Ownership | 오브젝트의 주인(Owner) 개념 |
🕹️ 실습 (Netcode)
- https://assetstore.unity.com/packages/essentials/starter-assets-thirdperson-updates-in-new-charactercontroller-pa-196526 설치
- Playground씬 실행 잘 되는지 테스트
Edit - Project Settings - Services- Window - Multiplayer Center에서 Game Specifications 설정 후 Install
- Multiplayer Widgets - Create Sessions, Join Session By Code, Quick Join Session, Session List, Leave Session, Session Player List, Show Session Code
- Window- Multiplayer - Multiplayer Play Mode 후 다른 Player 체크
- Button 추가 후 StartGame.cs와 연결
- Build Profiles에 Playground 씬 추가
[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; // 반환 상태 설정
}
'Unity > 멋쟁이사자처럼' 카테고리의 다른 글
멋쟁이 사자처럼 부트캠프 유니티 게임 개발 4기 250516 회고 (0) | 2025.05.19 |
---|---|
멋쟁이 사자처럼 부트캠프 유니티 게임 개발 4기 250513 회고 (0) | 2025.05.19 |
멋쟁이 사자처럼 부트캠프 유니티 게임 개발 4기 36일차 회고 (0) | 2025.04.17 |
멋쟁이 사자처럼 부트캠프 유니티 게임 개발 4기 35일차 회고 (0) | 2025.04.17 |
멋쟁이 사자처럼 부트캠프 유니티 게임 개발 4기 34일차 회고 (0) | 2025.04.10 |