기본 이동, 네트워크, 카메라 설정 + 기본 애셋 추가
This commit is contained in:
26
Assets/Scripts/AutoHost.cs
Normal file
26
Assets/Scripts/AutoHost.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using UnityEngine;
|
||||
using Unity.Netcode;
|
||||
|
||||
public class AutoHost : MonoBehaviour
|
||||
{
|
||||
// 에디터에서만 작동하도록 설정
|
||||
void Start()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// 1. NetworkManager가 씬에 존재하는지 확인
|
||||
if (NetworkManager.Singleton != null)
|
||||
{
|
||||
// 2. 이미 서버나 클라이언트가 실행 중이 아닐 때만 실행
|
||||
if (!NetworkManager.Singleton.IsServer && !NetworkManager.Singleton.IsClient)
|
||||
{
|
||||
NetworkManager.Singleton.StartHost();
|
||||
Debug.Log("<color=yellow><b>[AutoHost]</b> 에디터 전용 호스트 자동 시작됨</color>");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("[AutoHost] NetworkManager를 찾을 수 없습니다!");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
2
Assets/Scripts/AutoHost.cs.meta
Normal file
2
Assets/Scripts/AutoHost.cs.meta
Normal file
@@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: f0ad96fafd5ac4f4db7e02424e132b89
|
||||
35
Assets/Scripts/NetworkManagerUI.cs
Normal file
35
Assets/Scripts/NetworkManagerUI.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class NetworkManagerUI : MonoBehaviour
|
||||
{
|
||||
[Header("UI Buttons")]
|
||||
[SerializeField] private Button hostButton;
|
||||
[SerializeField] private Button clientButton;
|
||||
[SerializeField] private Button serverButton;
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (hostButton != null)
|
||||
hostButton.onClick.AddListener(() => NetworkManager.Singleton.StartHost());
|
||||
|
||||
if (clientButton != null)
|
||||
clientButton.onClick.AddListener(() => NetworkManager.Singleton.StartClient());
|
||||
|
||||
if (serverButton != null)
|
||||
serverButton.onClick.AddListener(() => NetworkManager.Singleton.StartServer());
|
||||
}
|
||||
|
||||
private void OnDestroy()
|
||||
{
|
||||
if (hostButton != null)
|
||||
hostButton.onClick.RemoveAllListeners();
|
||||
|
||||
if (clientButton != null)
|
||||
clientButton.onClick.RemoveAllListeners();
|
||||
|
||||
if (serverButton != null)
|
||||
serverButton.onClick.RemoveAllListeners();
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/NetworkManagerUI.cs.meta
Normal file
11
Assets/Scripts/NetworkManagerUI.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 8d7e6f5c4b3a2d1e0f9a8b7c6d5e4f3a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
75
Assets/Scripts/NetworkPlayerController.cs
Normal file
75
Assets/Scripts/NetworkPlayerController.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using Unity.Netcode;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using Unity.Cinemachine;
|
||||
|
||||
public class NetworkPlayerController : NetworkBehaviour
|
||||
{
|
||||
[Header("Movement Settings")]
|
||||
public float moveSpeed = 5f;
|
||||
public float rotationSpeed = 10f;
|
||||
public float jumpHeight = 1.5f;
|
||||
public float gravity = -19.62f;
|
||||
|
||||
[Header("Input")]
|
||||
[SerializeField] private InputActionAsset inputActions;
|
||||
|
||||
private Vector2 _moveInput;
|
||||
private CharacterController _controller;
|
||||
private PlayerInputActions _inputActions;
|
||||
private Animator _animator;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
_controller = GetComponent<CharacterController>();
|
||||
_animator = GetComponent<Animator>();
|
||||
}
|
||||
|
||||
// NGO 초기화
|
||||
public override void OnNetworkSpawn()
|
||||
{
|
||||
if (!IsOwner) return;
|
||||
|
||||
// 1. 씬에 있는 가상 카메라를 찾습니다.
|
||||
// Unity 6에서는 CinemachineVirtualCamera 대신 CinemachineCamera를 주로 사용합니다.
|
||||
var vcam = GameObject.FindAnyObjectByType<CinemachineCamera>();
|
||||
|
||||
if (vcam != null)
|
||||
{
|
||||
// 2. 카메라의 Follow와 LookAt 대상을 '나'로 설정합니다.
|
||||
vcam.Follow = transform;
|
||||
vcam.LookAt = transform;
|
||||
Debug.Log("<color=green>[Camera] 로컬 플레이어에게 카메라가 연결되었습니다.</color>");
|
||||
}
|
||||
|
||||
_inputActions = new PlayerInputActions();
|
||||
_inputActions.Enable();
|
||||
}
|
||||
|
||||
public override void OnNetworkDespawn()
|
||||
{
|
||||
if (IsOwner)
|
||||
{
|
||||
if (inputActions != null)
|
||||
{
|
||||
inputActions.Disable();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (!IsOwner) return;
|
||||
|
||||
_moveInput = _inputActions.Player.Move.ReadValue<Vector2>();
|
||||
Vector3 move = new Vector3(_moveInput.x, 0, _moveInput.y).normalized;
|
||||
if (move.magnitude >= 0.1f)
|
||||
{
|
||||
Quaternion targetRotation = Quaternion.LookRotation(move);
|
||||
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
|
||||
_controller.Move(move * moveSpeed * Time.deltaTime);
|
||||
}
|
||||
|
||||
_animator.SetFloat("MoveSpeed", move.magnitude);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/NetworkPlayerController.cs.meta
Normal file
11
Assets/Scripts/NetworkPlayerController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 9e8f7d6c5b4a3d2e1f0a9b8c7d6e5f4a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
34
Assets/Scripts/PlayerController.cs
Normal file
34
Assets/Scripts/PlayerController.cs
Normal file
@@ -0,0 +1,34 @@
|
||||
using UnityEngine;
|
||||
|
||||
[RequireComponent(typeof(Rigidbody))]
|
||||
public class PlayerController : MonoBehaviour
|
||||
{
|
||||
[Header("Movement Settings")]
|
||||
[SerializeField] private float moveSpeed = 5f;
|
||||
|
||||
private Rigidbody rb;
|
||||
private Vector3 moveDirection;
|
||||
|
||||
void Start()
|
||||
{
|
||||
rb = GetComponent<Rigidbody>();
|
||||
rb.constraints = RigidbodyConstraints.FreezeRotation;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
float horizontal = Input.GetAxisRaw("Horizontal");
|
||||
float vertical = Input.GetAxisRaw("Vertical");
|
||||
|
||||
moveDirection = new Vector3(horizontal, 0f, vertical).normalized;
|
||||
}
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
rb.linearVelocity = new Vector3(
|
||||
moveDirection.x * moveSpeed,
|
||||
rb.linearVelocity.y,
|
||||
moveDirection.z * moveSpeed
|
||||
);
|
||||
}
|
||||
}
|
||||
11
Assets/Scripts/PlayerController.cs.meta
Normal file
11
Assets/Scripts/PlayerController.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7a3e5b8c4d2f1a9e6b0c3d7e8f1a2b3c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user