二、删除Main Camera,将第三人称控制器中的MainCamera、PlayerCapsule、PlayerFollowCamera拖入Hierarchy窗口。
将Inspector窗口的CharacterControlller、ThirdPersonController、PlayerInput的勾取消;加入TransformComponent
将Player3拖入Project,成为Prefab。删除Hierarchy窗口的Player3.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Net.Component;
using Cinemachine;
using UnityEngine.InputSystem;
using StarterAssets;
public class GameManager : MonoBehaviour
{
public List<GameObject> PlayerPrefabs;
private void Start()
{
ClientManager.Instance.client.OnConnectedHandle += Connected;
}
private void Connected()
{
//生成 Index=2的模型Player
SpawnPlayer(2, Vector3.zero, Quaternion.identity);
}
public void SpawnPlayer(int _modelIndex, Vector3 position, Quaternion rotation)
{
NetworkTransformBase.Identity = ClientManager.UID;
GameObject _player = Instantiate(PlayerPrefabs[_modelIndex], position, rotation);
if (_modelIndex == 2) //使用第三人称控制器
{
_player.GetComponent<CharacterController>().enabled = true;
_player.GetComponent<ThirdPersonController>().enabled = true;
_player.GetComponent<PlayerInput>().enabled = true;
GameObject.Find("PlayerFollowCamera").GetComponent<CinemachineVirtualCamera>().Follow =
_player.transform.Find("PlayerCameraRoot").transform;
}
else
{
_player.GetComponent<PlayerController>().enabled = true;
}
}
}