HTC Vive Unity饥荒瞬移法杖怎么用插件该怎么用

用Unity开发HTC Vive:远处拖动3D物体如何实现?
VR视界萌萌君
HTC Vive 是由HTC与Valve联合开发的一款VR头显(虚拟现实头戴式显示器)产品,想必大家都玩到很多VIVE上的VR游戏了,那么如何使用Unity来进行VIVE上的VR游戏开发呢?
在项目中会需要拖动远处的物体,这种要如何去实现,下面就给大家介绍下在Unity HTC vive开发中远处拖动3D物体的教程。
效果如下,手柄射线照射到3D物体的时候,可以通过Trigger按钮抓住物体并拖动。
导入SDK:SteamVR Plugin和Vive Input Utility
删除场景中的默认摄像机,将CameraRig拖入场景
将VivePointers拖入场景
添加一个3d对象,并设置位置和大小
在物体上添加脚本Draggable,添加时会自动添加Rigidbody组件。运行即可。
<font color=#-14<font color=#-14<font color=#-13<font color=#-13<font color=#-10<font color=#-07<font color=#-08<font color=#-09<font color=#-27<font color=#-01
想知道在Unity中如何将游戏中角色对话的唇型与面部微表情达到次时代的水准吗?想知道...
近日,RLTY CHK工作室的联合创始人Nick Robinson发布了一篇文章,以媒体天师麦克卢汉...
VR设计师和开发者们开始着手如何让VR环境及体验更加有趣、逼真。以下是一些关于如何为room...
笔者在为《幻想装置》和《工作模拟》制作出混合现实的预告片以后,研究了更深层次的VR...
关于用EasyAR SDK 搭建AR 开发环境的教程,我已经写过很多了,不懂得朋友可以看下我之...修改于《用抛物线的点实现HTC Vive瞬移系统》,亲测可用
刚开始学HTC Vive在网上发现一个相关的教程,谢谢蛮牛的这位大神翻译。文章地址:http://www.manew.com/thread-.html
但是在其中有很多人说不能瞬移,也有人说可以,关于这个问题我研究了一下,接下来给大家解一下惑:
在ViveNavMesh脚本下的Linecast方法里,有一个NavMesh.SamplePosition的方法,目的是查找目标点最近的导航网格节点,脚本中的范围值为0.03,是一个非常小的值,所以造成了很多时候查找不到节点,而不能正常瞬移,只需要把值改大一点就可以正常运行了。
本人也是初学者,有问题大家也可以讨论一下,共同进步哦。下面粘贴ViveNavMesh脚本的修改代码(带注释):
using UnityE
using UnityEngine.S
using UnityEngine.R
using System.Collections.G
/// \brief A version of Unity's baked navmesh that is converted to a (serializable) component.
This allows the navmesh
used for Vive navigation to be separated form the AI Navmesh.
ViveNavMesh also handles the rendering of the
NavMesh grid in-game.
[AddComponentMenu("Vive Teleporter/Vive Nav Mesh")]
[RequireComponent(typeof(BorderRenderer))]
[ExecuteInEditMode]
public class ViveNavMesh : MonoBehaviour
/// Material used for the floor mesh when the user is selecting a point to teleport to
public Material GroundMaterial
get { return _GroundM }
Material old = _GroundM
_GroundMaterial =
if(_GroundMaterial != null)
_GroundMaterial.SetFloat(AlphaShaderID, GroundAlpha);
if (old != _GroundMaterial)
Cleanup();
[SerializeField]
private Material _GroundM
/// \brief The alpha (transparency) value of the rendered ground mesh)
/// \sa GroundMaterial
[Range(0,1)]
public float GroundAlpha = 1.0f;
private float LastGroundAlpha = 1.0f;
private int AlphaShaderID = -1;
/// A Mesh that represents the "Selectable" area of the world.
This is converted from Unity's NavMesh in ViveNavMeshEditor
public Mesh SelectableMesh
get { return _SelectableM }
set { _SelectableMesh = Cleanup(); } // Cleanup because we need to change the mesh inside command buffers
[SerializeField] [HideInInspector]
private Mesh _SelectableM
/// \brief The border points of SelectableMesh.
This is automatically generated in ViveNavMeshEditor.
/// This is an array of Vector3 arrays, where each Vector3 array is the points in a polyline.
These polylines combined
/// describe the borders of SelectableMesh.
We have to use BorderPointSets instead of a jagged Vector3[][] array because
/// Unity can't serialize jagged arrays for some reason.
public BorderPointSet[] SelectableMeshBorder
get { return _SelectableMeshB }
set { _SelectableMeshBorder = Border.Points = _SelectableMeshB }
[SerializeField] [HideInInspector]
private BorderPointSet[] _SelectableMeshB
[SerializeField] [HideInInspector]
private int _NavAreaMask = ~0; // Initialize to all
private BorderRenderer B
private Dictionary&Camera, CommandBuffer& cameras = new Dictionary&Camera, CommandBuffer&();
void Start () {
if (SelectableMesh == null)
SelectableMesh = new Mesh();
if (_SelectableMeshBorder == null)
_SelectableMeshBorder = new BorderPointSet[0];
Border = GetComponent&BorderRenderer&();
Border.Points = SelectableMeshB
AlphaShaderID = Shader.PropertyToID("_Alpha");
#if UNITY_EDITOR
UnityEditor.SceneView.RepaintAll();
void Update ()
// We have to detect changes this way instead of using properties because
// we want to be able to animate the alpha value with a Unity animator.
if (GroundAlpha != LastGroundAlpha && GroundMaterial != null)
GroundMaterial.SetFloat(AlphaShaderID, GroundAlpha);
LastGroundAlpha = GroundA
private void Cleanup()
foreach (var cam in cameras)
if (cam.Key)
cam.Key.RemoveCommandBuffer(CameraEvent.AfterForwardOpaque, cam.Value);
cameras.Clear();
public void OnEnable()
Cleanup();
public void OnDisable()
Cleanup();
void OnRenderObject()
// We have to use command buffers instead of Graphics.DrawMesh because of strange depth issues that I am experiencing
// with Graphics.Drawmesh (perhaps Graphics.DrawMesh is called before all opaque objects are rendered?)
var act = gameObject.activeInHierarchy &&
Cleanup();
// If _SelectableMesh == null there is a crash in Unity 5.4 beta (apparently you can't pass null to CommandBuffer::DrawMesh now).
if (!_SelectableMesh || !GroundMaterial)
var cam = Camera.
if (!cam || cam.cameraType == CameraType.Preview || ((1 && gameObject.layer) & Camera.current.cullingMask) == 0)
CommandBuffer buf =
if (cameras.ContainsKey(cam))
buf = new CommandBuffer();
// Note: Mesh is drawn slightly pushed upwards to avoid z-fighting issues
buf.DrawMesh(_SelectableMesh, Matrix4x4.TRS(Vector3.up * 0.005f, Quaternion.identity, Vector3.one), GroundMaterial, 0);
cameras[cam] =
cam.AddCommandBuffer(CameraEvent.AfterForwardOpaque, buf);
void OnValidate()
Border = GetComponent&BorderRenderer&();
Border.Points = SelectableMeshB
if(AlphaShaderID == -1)
AlphaShaderID = Shader.PropertyToID("_Alpha");
/// \brief Casts a ray against the Navmesh and attempts to calculate the ray's worldspace intersection with it.
/// This uses Physics raycasts to perform the raycast calculation, so the teleport surface must have a collider
/// on it.
/// \param p1 First (origin) point of ray
/// \param p2 Last (end) point of ray
/// \param pointOnNavmesh If the raycast hit something on the navmesh.
/// \param hitPoint If hit, the point of the hit.
Otherwise zero.
/// \return If the raycast hit something.
public bool Linecast(Vector3 p1, Vector3 p2, out bool pointOnNavmesh, out Vector3 hitPoint)
Vector3 dir = p2 - p1;
//p2为目标,p1为自身
float dist = dir.
if(Physics.Raycast(p1, dir, out hit, dist))
if(Vector3.Dot(Vector3.up, hit.normal) & 0.99f)
pointOnNavmesh =
hitPoint = hit.
hitPoint = hit.
NavMeshHit navH
//导航网格中可查询的最近的点
pointOnNavmesh = NavMesh.SamplePosition(hitPoint, out navHit, 0.3f, _NavAreaMask);
// This is necessary because NavMesh.SamplePosition does a sphere intersection, not a projection onto the mesh or
// something like that.
This means that in some scenarios you can have a point that's not actually on/above
// the NavMesh but is right next to it.
However, if the point is above a Navmesh position that has a normal
// of (0,1,0) we can assume that the closest position on the Navmesh to any point has the same x/z coordinates
// UNLESS that point isn't on top of the Navmesh.
if( !Mathf.Approximately(navHit.position.x, hitPoint.x) ||
!Mathf.Approximately(navHit.position.z, hitPoint.z))
pointOnNavmesh =
pointOnNavmesh =
hitPoint = Vector3.
[System.Serializable]
public class BorderPointSet
public Vector3[] P
public BorderPointSet(Vector3[] Points)
this.Points = P
看过本文的人也看了:
我要留言技术领域:
取消收藏确定要取消收藏吗?
删除图谱提示你保存在该图谱下的知识内容也会被删除,建议你先将内容移到其他图谱中。你确定要删除知识图谱及其内容吗?
删除节点提示无法删除该知识节点,因该节点下仍保存有相关知识内容!
删除节点提示你确定要删除该知识节点吗?他的最新文章
他的热门文章
您举报文章:
举报原因:
原文地址:
原因补充:
(最多只允许输入30个字)请完成以下验证码
您当前的位置: &
用Unity开发HTC VIVE——手柄控制篇
查看: 4359|
写这篇文章的原因主要是因为现在虚拟现实非常的火爆但目前主流的虚拟现实设备(HTC VIVE)的教程却少的可怜,这个我深有体会。所以,我想将我平时开发中遇到的问题以及解决方法记录下来,分享给大家,若其中有什么错误或者大家有什么更好的方案也请大家指出,大家互相学习,哈哈。好了直接上代码。using UnityE
using System.C
public class shoubingkongzhi : MonoBehaviour {
SteamVR_TrackedO
void Awake()
//获取手柄
tracked = GetComponent();
// Update is called once per frame
void FixedUpdate()
var device = SteamVR_Controller.Input((int)tracked.index);
if (device.GetPressDown(SteamVR_Controller.ButtonMask.Touchpad))
Debug.Log("按下圆盘");
else if (device.GetPressDown(SteamVR_Controller.ButtonMask.Trigger))
Debug.Log("按下扳机键");
else if (device.GetPressDown(SteamVR_Controller.ButtonMask.Grip))
Debug.Log("按下手柄侧键");
else if (device.GetPressDown(SteamVR_Controller.ButtonMask.ApplicationMenu))
Debug.Log("按下手柄菜单键");
else if (device.GetPressDown(SteamVR_Controller.ButtonMask.ApplicationMenu))
Debug.Log("按下手柄菜单键");
Debug.Log("按下手柄菜单键");}}}以上都是HTC VIVE手柄中按键按下的代码。其他还有:按键松开—device.GetPressUp(SteamVR_Controller.ButtonMask.Trigger);按键长安—device.GetPress(SteamVR_Controller.ButtonMask.Trigger);按键按下还有另一种方式,但是我自我感觉用着很别扭,没上述的好。device.GetTouchDown(SteamVR_Controller.ButtonMask.ApplicationMenu);其他用法与GetPressDown()类似。
52VR.COM微信扫一扫
专注于VR的学习、开发和人才交流
&津ICP备号扫一扫,访问微社区
后使用快捷导航没有帐号?
签到成功!您今天第{todayrank}个签到,签到排名竞争激烈,记得每天都来签到哦!已连续签到:{constant}天,累计签到:{days}天
当前位置: &
查看: 1806|回复: 9
怎么用HTC_vive的手柄实现开门
133/50排名<font color="#FF昨日变化18主题帖子积分
注册看看, 积分 33, 距离下一级还需 17 积分
注册看看, 积分 33, 距离下一级还需 17 积分
在线时间13 小时
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
才可以下载或查看,没有帐号?
求教:如何实现用HTC_vive的手柄去触碰门把,通过手柄的拉动,来实现门的旋转,就和现实中自己用手去开门的效果一样,求教,在线等,谢谢
每日推荐:
16排名<font color="#FF昨日变化主题帖子积分
蛮牛币47501
在线时间3392 小时
亲~希望这个帖子能对你有所帮助~
请教怎么实现用控制器推拉门的效果?
每日推荐:
回帖是美德~是一种行动认可和支持~希望更多蛮牛小伙伴参与到支持行动中~
133/50排名<font color="#FF昨日变化18主题帖子积分
注册看看, 积分 33, 距离下一级还需 17 积分
注册看看, 积分 33, 距离下一级还需 17 积分
在线时间13 小时
亲~希望这个帖子能对你有所帮助~
请教怎么实现用控制器推拉门的效果?
好的 谢谢了 我去研究下铰链
每日推荐:
4437/500排名<font color="#FF昨日变化2主题帖子积分
四处流浪, 积分 437, 距离下一级还需 63 积分
四处流浪, 积分 437, 距离下一级还需 63 积分
在线时间168 小时
VRTK里有个开门的Demo,你可以参考下
每日推荐:
133/50排名<font color="#FF昨日变化18主题帖子积分
注册看看, 积分 33, 距离下一级还需 17 积分
注册看看, 积分 33, 距离下一级还需 17 积分
在线时间13 小时
谢谢 已经会了
每日推荐:
256/150排名<font color="#FF昨日变化13主题帖子积分
初来乍到, 积分 56, 距离下一级还需 94 积分
初来乍到, 积分 56, 距离下一级还需 94 积分
在线时间19 小时
楼主怎么会做的,分享一下经验,过程
每日推荐:
149/50排名<font color="#FF昨日变化24主题帖子积分
注册看看, 积分 49, 距离下一级还需 1 积分
注册看看, 积分 49, 距离下一级还需 1 积分
在线时间25 小时
来自Mobile---
小志志 发表于
谢谢 已经会了
小志志哥,怎么实现的呀?求助
每日推荐:
149/50排名<font color="#FF昨日变化24主题帖子积分
注册看看, 积分 49, 距离下一级还需 1 积分
注册看看, 积分 49, 距离下一级还需 1 积分
在线时间25 小时
来自Mobile---
东去 发表于
VRTK里有个开门的Demo,你可以参考下
那个demo没用到铰链吗?
每日推荐:
4343/500排名<font color="#FF昨日变化4主题帖子积分
四处流浪, 积分 343, 距离下一级还需 157 积分
四处流浪, 积分 343, 距离下一级还需 157 积分
在线时间189 小时
小志志哥,怎么实现的呀?求助
看一下VRTK里的场景 有个开门的 看看他上面挂的脚本 被物体加碰撞铰链什么的 仿照着做的
每日推荐:
5652/1000排名<font color="#FF昨日变化46主题帖子积分
熟悉之中, 积分 652, 距离下一级还需 348 积分
熟悉之中, 积分 652, 距离下一级还需 348 积分
蛮牛币1082
在线时间172 小时
vrtk 里面有一个开门的
每日推荐:
七夕浪漫情人
2015年蛮牛社区浪漫七夕 最美情话活动获奖者
在“新人报到 ”版块发过自己的照片

我要回帖

更多关于 芈月一技能瞬移怎么用 的文章

 

随机推荐