论坛元老
兴趣点(最多三项):
Unity
UDK|Unreal
CryEngine
- 私信
|
发表时间 : 2017-2-16 16:57:56
|
浏览 : 59904 评论 : 207
关键词:Unity3D + HTC Vive + Multiplayer
如何使用unity 自带的网络组件来实现VR多人联网,实现局域网联网,广域网的话貌似也可以,但是应该还是需要一个服务器人员,其中的血量控制代码:
- public class Health : NetworkBehaviour
- {
- public const int maxHealth = 100;
- [SyncVar (hook = "OnHealthChange")] public int health = maxHealth;
- public RectTransform healthBar;
- public bool destroyOnDeath;
- private NetworkStartPosition[] spawnPoints;
- void Start()
- {
- if (isLocalPlayer)
- {
- spawnPoints = FindObjectsOfType<NetworkStartPosition>();
- }
- }
- public void TakeDamage(int amount)
- {
- if (!isServer) return;
- health -= amount;
- if (health <= 0)
- {
- if (destroyOnDeath)
- {
- Destroy(gameObject);
- }
- else
- {
- health = maxHealth;
- RpcRespawn();
- }
-
- }
- }
- void OnHealthChange(int hlth)
- {
- healthBar.sizeDelta = new Vector2(hlth * 2, healthBar.sizeDelta.y);
- }
- [ClientRpc]
- void RpcRespawn()
- {
- if (isLocalPlayer)
- {
- Vector3 spawnPoint = Vector3.zero;
- if(spawnPoints != null && spawnPoints.Length>0)
- {
- spawnPoint = spawnPoints[UnityEngine.Random.Range(0, spawnPoints.Length)].transform.position;
- }
- transform.position = spawnPoint;
-
- }
- }
- }
复制代码
整个工程很简单,只有初步的联网,大家仅做参考学习:
链接:http://pan.baidu.com/s/1eSJv2Mm
|
评分
-
查看全部评分
|