Unet学习

xiaoxiao2021-02-28  16

1.创建NetworkManager(空物体),在上面添加NetworkManager组件,添加NetWorkManagerHUD组件

2.创建预制体PlayerPrefab,在上面添加NetworkIdentity,勾选Local Player Authority,在NetWorkManager组件下的SpawnInfo里,将预制件拖入PlayerPrefab上

3。控制移动:添加PlayerController脚本,

4.在Player预制体上添加NetworkTransform,实时同步

5.在子弹上面加NetworkIdentity,同时添加NetworkTransform,将子弹添加到SpawnInfo中

6.制作敌人,添加预制件Enemy

7.添加EnemySpawner

8.添加两个NetworkStartPosition,在NetworkManager里选择RoundRobin

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; public class Enemyspawner : NetworkBehaviour { public GameObject enemyPrefab; public int numberOfEnemies; public override void OnStartServer() { for(int i=0;i<numberOfEnemies;i++) { Vector3 position = new Vector3(Random.Range(-6, 6), 0, Random.Range(-6, 6)); Quaternion rotation = Quaternion.Euler(0, Random.Range(0, 360), 0); GameObject enemy = GameObject.Instantiate(enemyPrefab, position, rotation) as GameObject; NetworkServer.Spawn(enemy); } } } using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Networking; public class PlayerController : NetworkBehaviour { public GameObject bulletPrefab; public Transform firePos; // Update is called once per frame void Update () { if(isLocalPlayer==false) { return; } float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); transform.Rotate(Vector3.up * h * 120 * Time.deltaTime); transform.Translate(Vector3.forward * v * 3 *Time.deltaTime); if(Input.GetKeyDown(KeyCode.Space)) { CmdFire(); } } [Command] //called in client run in server void CmdFire() //这个方法在Server里面调用 { //子弹生成 需要Server完成 , 然后把子弹同步到Client中 GameObject bullet = GameObject.Instantiate(bulletPrefab, firePos.position, firePos.rotation); bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * 10; Destroy(bullet, 2); NetworkServer.Spawn(bullet); //同步到各个客户端 } public override void OnStartLocalPlayer() { //这个方法只会在本地角色那里调用 当角色被创建的时候 GetComponent<MeshRenderer>().material.color = Color.blue; } }

using System.Collections; using System.Collections.Generic; using UnityEngine; public class Bullet : MonoBehaviour { private void OnCollisionEnter(Collision collision) { GameObject hit = collision.gameObject; Health headlth = hit.GetComponent<Health>(); if(headlth!=null) { headlth.TakeDamage(10); } Destroy(this.gameObject); } }

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.Networking; public class Health : NetworkBehaviour { public const int maxHealth = 100; [SyncVar(hook ="OnChangeHealth")] public int currentHealth = maxHealth; public Slider healthSlider; public bool DestroyOnDeath = false; private NetworkStartPosition[] spawnPoints; private void Start() { if(isLocalPlayer) { spawnPoints = FindObjectsOfType<NetworkStartPosition>(); } } public void TakeDamage(int damage) { if(isServer==false) //血量的处理只在服务器端执行 { return; } currentHealth -= damage; if(currentHealth<=0) { if(DestroyOnDeath) { Destroy(this.gameObject);return; } currentHealth = maxHealth; Debug.Log("Dead"); RpcRespawn(); } } void OnChangeHealth(int health) { healthSlider.value = health / (float)maxHealth; } [ClientRpc] //只在客户端下进行 void RpcRespawn() { if(isLocalPlayer==false) //是否是本地玩家 { return; } Vector3 spawnPosition = Vector3.zero; if(spawnPoints!=null && spawnPoints.Length>0) { spawnPosition = spawnPoints[Random.Range(0, spawnPoints.Length)].transform.position; } transform.position = spawnPosition; } }

using System.Collections; using System.Collections.Generic; using UnityEngine; public class LookAt : MonoBehaviour { // Update is called once per frame void Update () { transform.LookAt(Camera.main.transform); } }

转载请注明原文地址: https://www.6miu.com/read-2250302.html

最新回复(0)