Unity + Networking + HtcVive 实现多人VR

1、在AssestStore下载SteamVR Plugins (免费)插件并导入到当前项目。

Unity + Networking + HtcVive 实现多人VR_第1张图片

导入成功

Unity + Networking + HtcVive 实现多人VR_第2张图片

2、创建一个空物体并重命名为NetworkManger,并为其添加Network Manger组件和Network Manger HUD组件

Unity + Networking + HtcVive 实现多人VR_第3张图片Unity + Networking + HtcVive 实现多人VR_第4张图片

3、制作预制体,

Unity + Networking + HtcVive 实现多人VR_第5张图片

4、给父物体添加下面这四个组件

Unity + Networking + HtcVive 实现多人VR_第6张图片

5、给两只手添加下面两个组件,左右手相同

Unity + Networking + HtcVive 实现多人VR_第7张图片

6、将左右手分别拖动到下面,注意只能从Hierachy面板上来拖动。

Unity + Networking + HtcVive 实现多人VR_第8张图片

7、将预制体放到Network的 Spawn Info中

Unity + Networking + HtcVive 实现多人VR_第9张图片


玩家预制体的代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;

public class VRPlayerController : NetworkBehaviour {


	public GameObject vrCameraRig;
	public GameObject Hand;
	private GameObject vrCameraRigInstance;
	private GameObject rightVRHand;
	private GameObject leftVRHand;
	private Transform Localright;
	private Transform Localleft;


	public override void OnStartLocalPlayer ()
	{

		GetComponent ().material.color = Color.blue;

		if (!isClient) {
			return;
		}

		DestroyImmediate (Camera.main.gameObject);


		vrCameraRigInstance = (GameObject)Instantiate (
			vrCameraRig,
			transform.position,
			transform.rotation);

		Transform bodyOfVrPlayer = transform.Find ("VRPlayerBody");
		if (bodyOfVrPlayer != null)
			bodyOfVrPlayer.parent = null;

		GameObject head = vrCameraRigInstance.GetComponentInChildren ().gameObject;
		transform.parent = head.transform;
		transform.localPosition = new Vector3(0f, -0.03f, -0.06f);

	}

	// Use this for initialization
	void Start () {
		
		Localleft = GetComponentsInChildren ()[2];
		Localright = GetComponentsInChildren ()[3];
	}

	// Update is called once per frame
	void Update () {

		var controllers =  vrCameraRigInstance.GetComponentsInChildren ();

		if (controllers != null && controllers.Length == 2 && controllers[0] != null && controllers[1] != null)
		{
			Localleft.position = controllers[0].transform.position;
			Localleft.rotation = controllers[0].transform.rotation;
			Localright.position = controllers[1].transform.position;
			Localright.rotation = controllers[1].transform.rotation;
		}
		
	}
}

将代码挂载到VRPlayer上即可。

你可能感兴趣的:(VR)