일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- Default
- fusion
- 브이알챗
- 유니티 네비매쉬
- 유니티
- Animation
- unity
- 티스토리챌린지
- udon#
- c#
- VRChat
- navmash
- 제네릭
- unityai
- pothon
- sendcustomnetworkevent
- navmashagent
- 오블완
- CS
- c
- u#
- animator
- 포톤
- generic
- typeof
- Today
- Total
대마법사의 개발 블로그랄까?
유니티 [포톤] Photon Fusion Shared Mode 본문
해당 포스트에서는 멀티플레이 연동과 간단한 움직임만 구현해볼것이다.
(Photon 설치 및 세팅과정은 생략)
시작하기
우선 Scene에 Network 세팅 오브젝트를 생성해주자
Hierachy 에서 Fusion / Scene / Setup Networking in the Scene
그러면 아래와 같이 두개의 GameObject가 생성된다.
- Prototype Network Start
- Prototype Runner
Prototype Network Start
게임을 시작할때 NetworkMode 설정을 할수 있도록 해준다. 자세한 설명은 밑에서....
Prototype Runner
메인이다.
Player Join, Player Left 등 중요한 네트워크에 관한 콜백함수들을 다루고있다.
자그럼 씬 Setting은 끝났다
그러면 플레이어를 스폰해보자
PlayerSpawner
using UnityEngine;
using Fusion;
public class PlayerSpawner : SimulationBehaviour, IPlayerJoined
{
public GameObject playerPrefab; //스폰할 오브젝트
public void PlayerJoined(PlayerRef player)
{
if (Runner.LocalPlayer == player)
{
Runner.Spawn(playerPrefab, Vector3.zero, Quaternion.identity, player);
//스폰할 플레이어, 스폰지점, 회전,권한
}
}
}
IPlayerJoined 인터페이스를 구현하여 PlayerJoined에서 플레이어가 새로 접속하면 해당 플레이어의 플레이어블 캐릭터를 스폰하는 코드다
해당코드를 Prototype Runner에 컴포넌트로 추가해주자
PlayerMovement
자 유니티를 해왔다면 수백번은 봤을 이동 코드가 대부분이다
눈대중으로 훑어보고 아래에서 스니펫을 보여주겠다
using Fusion;
using UnityEngine;
public class PlayerMovement : NetworkBehaviour
{
private Animator anim;
[SerializeField] CharacterController controller;
public float speed = 5f;
[Header("Setting")]
[SerializeField] private float forwardSpeed = 6;
[SerializeField] private float backwardSpeed = 3;
[SerializeField] private float sideSpeed = 4;
[SerializeField] private float rotateSpeed = 100;
private void Awake()
{
controller = GetComponent<CharacterController>();
anim = GetComponent<Animator>();
}
public override void FixedUpdateNetwork()
{
if (HasInputAuthority == false)
return;
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
float mouseX = Input.GetAxis("Mouse X");
float mouseY = Input.GetAxis("Mouse Y");
{
Vector3 move = new Vector3(horizontal, 0, vertical);
Vector3 rot = new Vector3(mouseX, 0, mouseY);
Move(move);
Rotate(rot);
}
}
private void Move(Vector3 dir)
{
float h = dir.x;
float v = dir.z;
if (h != 0 || v != 0)
{
anim.SetBool("isWalk", true);
}
else
{
anim.SetBool("isWalk", false);
}
Vector3 movement = transform.forward * v * forwardSpeed + transform.right * h * sideSpeed;
controller.Move(movement * Time.deltaTime);
Debug.Log(movement);
anim.SetFloat("x", h);
anim.SetFloat("y", v);
}
private void Rotate(Vector3 rot)
{
transform.Rotate(Vector3.up * rot.x);
}
}
스니펫
public override void FixedUpdateNetwork()
{
//권한 확인
if (HasInputAuthority == false)
return;
// 이동 함수 구현
//Move();
}
FixedUpdateNetwork()
우리는 NetworkBehaviour 를 상속받았다.
기존에 사용하는 FiexdUpdate를 사용하지 말고
FixedUpdateNetwork() 를 통해 이동함수를 구현해야한다.
Bool HasInputAuthority
해당 오브젝트의 주인을 확인해야한다.
자기자신의 플레이어블 오브젝트만 컨트롤해야하니까
체크를 하지않는다면 내가 다른플레이어의 캐릭터를 움직이는 기괴한 현상을 볼 수 있을것이다.
Player Prefab
플레이할 캐릭터를 프리팹으로 만들어야한다.
아래의 컴포넌트들을 추가해준다.
- NetworkTransform
- Network Object
- Character Controller
- Player Movement (방금 위에서 만든거)
자 만들었다면
PlayerSpawner의 Player Prefab에 할당해주자
그럼 모든 준비는 끝났다
테스트 ㄱㄱ
시작하면 위와같은 UI가 생성된다.
Started Shared Client를 클릭하자
조금만 기다리면 직접만든 플레이어가 생성될것이다.
또다른 인스턴스에서 똑같이 SharedClient를 클릭해보자
빡빡이 아저씨가 두명이다.
각각의 빡빡이 아저씨가 컨트롤이 잘된다.
성공!!!
'Unity 한테 정복당하기 > Photon 멀티플레이 게임 제작' 카테고리의 다른 글
[Photon] Fusion Animation (0) | 2025.05.26 |
---|---|
Unity 멀티플레이 Photon Fusion 겉핥기 (5) | 2025.05.05 |