码迷,mamicode.com
首页 > 编程语言 > 详细

htc vive 游戏切换武器功能思路以及demo(unity,c#)

时间:2016-06-16 15:06:20      阅读:958      评论:0      收藏:0      [点我收藏+]

标签:

</pre>个人感觉vr游戏或者展示类程序的灵魂就是真实感以及身临其境的感觉,如果场景中加入了大量的ui会破坏真实感,影响感觉,因此我觉得ui这种东西在vr里应该尽量转化为现实物体,比如显示屏,按钮等,一来增加了交互,二来也在增加真实感的同时也实现了ui的功能。<span style="font-family:黑体; word-wrap:break-word; color:rgb(105,105,105); font-size:16px; line-height:25px"><span style="font-size:24px; word-wrap:break-word"><span style="word-wrap:break-word; font-weight:700">  以上是废话,意思就是ui最好做成3d的,个人感觉。。。。。。<br style="word-wrap:break-word" /></span></span></span><p><span style="font-family:黑体; word-wrap:break-word; color:rgb(105,105,105); font-size:16px; line-height:25px"><span style="font-size:24px; word-wrap:break-word"><span style="word-wrap:break-word; font-weight:700">  htc vive 在steam上有款游戏叫HoverJunkers,个人感觉是目前所有htc viveFPS游戏里的顶端作品了,沉浸感很强,虽然是虚构出来的科幻的末世风格,枪械手感做的也非常有感觉,感觉这款游戏里的换枪功能做的手感不错,所以用自己的思路模仿着做了一下,大体实现了功,界面简陋,代码写的也太嫩,有很多需要优化的地方,因此仅仅提供一个思路,不仅仅可以换钱,也可以当做其他的功能使用,算是抛砖引玉,大家有体验更好的这类交互希望也一起分享一下.</span></span></span></p><p><span style="font-family:黑体; word-wrap:break-word; color:rgb(105,105,105); font-size:16px; line-height:25px"><span style="font-size:24px; word-wrap:break-word"><span style="word-wrap:break-word; font-weight:700"><span style="color:rgb(105,105,105); line-height:25px; word-wrap:break-word; font-family:'microsoft yahei'; font-size:16px"><strong>题外:我的微博 “ </strong></span><span style="color:rgb(105,105,105); line-height:25px; font-size:32px; word-wrap:break-word; font-family:'microsoft yahei'"><strong><span style="word-wrap:break-word"><span style="color:red; word-wrap:break-word">黑石铸造厂厂长</span></span></strong></span><span style="color:rgb(105,105,105); line-height:25px; word-wrap:break-word; font-family:'microsoft yahei'; font-size:16px"><strong> ”,想要个大 v粉丝不够啊 ,大家互粉一下呗</strong></span></span></span></span></p><p><span style="font-family:黑体; word-wrap:break-word; color:rgb(105,105,105); font-size:16px; line-height:25px"><span style="font-size:24px; word-wrap:break-word"><span style="word-wrap:break-word; font-weight:700"><span style="color:rgb(105,105,105); line-height:25px; word-wrap:break-word; font-family:'microsoft yahei'; font-size:16px"><strong><img src="http://img.manew.com/data/attachment/forum/201606/15/113709t3cxxl1ss1k9s2uy.jpg" alt="" /></strong></span></span></span></span></p><p><span style="font-family:黑体; word-wrap:break-word; color:rgb(105,105,105); font-size:16px; line-height:25px"><span style="font-size:24px; word-wrap:break-word"><span style="word-wrap:break-word; font-weight:700"><span style="color:rgb(105,105,105); line-height:25px; word-wrap:break-word; font-family:'microsoft yahei'; font-size:16px"><strong><img src="http://img.manew.com/data/attachment/forum/201606/15/113704bo1yskwrrfmlm321.jpg" alt="" /></strong></span></span></span></span></p><p><span style="font-family:黑体; word-wrap:break-word; color:rgb(105,105,105); font-size:16px; line-height:25px"><span style="font-size:24px; word-wrap:break-word"><span style="word-wrap:break-word; font-weight:700"><span style="color:rgb(105,105,105); line-height:25px; word-wrap:break-word; font-family:'microsoft yahei'; font-size:16px"><strong><span style="word-wrap:break-word; font-weight:700; color:rgb(105,105,105); font-family:'microsoft yahei'; font-size:16px; line-height:25px">这就是hoverjunkers的换弹功能,按住手柄上的menu键(就是圆盘上面那个键),就会弹出一个3d的ui,手柄指向选择的武器松开menu键就会获得选择的武器(选中武器高亮)</span><br style="word-wrap:break-word; color:rgb(105,105,105); font-family:'microsoft yahei'; font-size:16px; line-height:25px" /><span style="word-wrap:break-word; font-weight:700; color:rgb(105,105,105); font-family:'microsoft yahei'; font-size:16px; line-height:25px">现在就通过我的思路简单实现一下以上功能</span></strong></span></span></span></span></p><p><span style="font-family:黑体; word-wrap:break-word; color:rgb(105,105,105); font-size:16px; line-height:25px"><span style="font-size:24px; word-wrap:break-word"><span style="word-wrap:break-word; font-weight:700"><span style="color:rgb(105,105,105); line-height:25px; word-wrap:break-word; font-family:'microsoft yahei'; font-size:16px"><strong><span style="word-wrap:break-word; font-weight:700; color:rgb(105,105,105); font-family:'microsoft yahei'; font-size:16px; line-height:25px"><img src="http://img.manew.com/data/attachment/forum/201606/15/115549fvk577erhjv5kr75.jpg" alt="" /></span></strong></span></span></span></span></p><p><span style="font-family:黑体; word-wrap:break-word; color:rgb(105,105,105); font-size:16px; line-height:25px"><span style="font-size:24px; word-wrap:break-word"><span style="word-wrap:break-word; font-weight:700"><span style="color:rgb(105,105,105); line-height:25px; word-wrap:break-word; font-family:'microsoft yahei'; font-size:16px"><strong><span style="word-wrap:break-word; font-weight:700; color:rgb(105,105,105); font-family:'microsoft yahei'; font-size:16px; line-height:25px"><img src="http://img.manew.com/data/attachment/forum/201606/15/115550ukpk5yy65qkdenof.jpg" alt="" /></span></strong></span></span></span></span></p><p><span style="font-family:黑体; word-wrap:break-word; color:rgb(105,105,105); font-size:16px; line-height:25px"><span style="font-size:24px; word-wrap:break-word"><span style="word-wrap:break-word; font-weight:700"><span style="color:rgb(105,105,105); line-height:25px; word-wrap:break-word; font-family:'microsoft yahei'; font-size:16px"><strong><span style="word-wrap:break-word; font-weight:700; color:rgb(105,105,105); font-family:'microsoft yahei'; font-size:16px; line-height:25px"><img src="http://img.manew.com/data/attachment/forum/201606/15/115551mwtbge0wow0debsz.jpg" alt="" /></span></strong></span></span></span></span></p><p><span style="font-family:黑体; word-wrap:break-word; color:rgb(105,105,105); font-size:16px; line-height:25px"><span style="font-size:24px; word-wrap:break-word"><span style="word-wrap:break-word; font-weight:700"><span style="color:rgb(105,105,105); line-height:25px; word-wrap:break-word; font-family:'microsoft yahei'; font-size:16px"><strong><span style="word-wrap:break-word; font-weight:700; color:rgb(105,105,105); font-family:'microsoft yahei'; font-size:16px; line-height:25px"><img src="http://img.manew.com/data/attachment/forum/201606/15/115551bbtti6jj6o4teo2t.jpg" alt="" /></span></strong></span></span></span></span></p><p><span style="font-family:黑体; word-wrap:break-word; color:rgb(105,105,105); font-size:16px; line-height:25px"><span style="font-size:24px; word-wrap:break-word"><span style="word-wrap:break-word; font-weight:700"><span style="color:rgb(105,105,105); font-weight:bold; line-height:25px; word-wrap:break-word; font-family:'microsoft yahei'; font-size:16px"><span style="word-wrap:break-word; font-weight:700; color:rgb(105,105,105); font-family:'microsoft yahei'; font-size:16px; line-height:25px"><span style="word-wrap:break-word; font-weight:700; color:rgb(105,105,105); font-family:'microsoft yahei'; font-size:16px; line-height:25px">这就是制作demo的效果</span><br style="word-wrap:break-word; color:rgb(105,105,105); font-family:'microsoft yahei'; font-size:16px; line-height:25px" /><span style="word-wrap:break-word; font-weight:700; color:rgb(105,105,105); font-family:'microsoft yahei'; font-size:16px; line-height:25px"><span style="font-size:24px; word-wrap:break-word">原理就是事先绑好武器与手柄,布置好3d的武器列表,做成什么效果看美工了(调整格子外貌,normal材质和高亮材质等工作),从手柄发射一个ray,ray碰撞武器列表里的格子,碰到对应的格子激活对应的武器,其他的取消激活。</span></span><br style="word-wrap:break-word; color:rgb(105,105,105); font-family:'microsoft yahei'; font-size:16px; line-height:25px" /><span style="word-wrap:break-word; font-weight:700; color:rgb(105,105,105); font-family:'microsoft yahei'; font-size:16px; line-height:25px"><span style="font-size:24px; word-wrap:break-word">说到这里好像做了个很low的东西,毕竟效果是实现了,新手就是这么容易满足,当做笔记吧, 哈哈哈哈哈</span></span><br style="word-wrap:break-word; color:rgb(105,105,105); font-family:'microsoft yahei'; font-size:16px; line-height:25px" /><span style="word-wrap:break-word; font-weight:700; color:rgb(105,105,105); font-family:'microsoft yahei'; font-size:16px; line-height:25px"><span style="font-size:24px; word-wrap:break-word">主要的代码贴一下:</span></span><br style="word-wrap:break-word; color:rgb(105,105,105); font-family:'microsoft yahei'; font-size:16px; line-height:25px" /><span style="font-size:24px; word-wrap:break-word; color:rgb(105,105,105); font-family:'microsoft yahei'; line-height:25px"><span style="word-wrap:break-word; font-weight:700">我是新手,我会努力学习规范代码优化代码的,所以大家先凑合看</span></span></span></span></span></span></span></p><p><span style="font-family:黑体; word-wrap:break-word; color:rgb(105,105,105); font-size:16px; line-height:25px"><span style="font-size:24px; word-wrap:break-word"><span style="word-wrap:break-word; font-weight:700"><span style="color:rgb(105,105,105); font-weight:bold; line-height:25px; word-wrap:break-word; font-family:'microsoft yahei'; font-size:16px"><span style="word-wrap:break-word; font-weight:700; color:rgb(105,105,105); font-family:'microsoft yahei'; font-size:16px; line-height:25px"><span style="font-size:24px; word-wrap:break-word; color:rgb(105,105,105); font-family:'microsoft yahei'; line-height:25px"><span style="word-wrap:break-word; font-weight:700"></span></span></span></span></span></span></span></p><pre name="code" class="csharp">using UnityEngine;
using System.Collections;
 
public class chooseWeapon : MonoBehaviour {
    //手柄
    SteamVR_TrackedObject trackdeObjec;
 
    //武器列表
    public GameObject weaponMenu;
    //武器列表中的枪选项
    public GameObject weaponMenuGun;
    //武器列表中的刀选项
    public GameObject weaponMeunKnife;
    //手上的武器:刀
    public GameObject kinfe;
    //手上的武器:枪
    public GameObject gun;
    //从手柄发出射线来选择武器
    //射线起始位置
    public Transform rayStart;
    //射线方向
    public Transform rayTarget;
    //列表起始颜色
    Color menuNormalColor;
    //列表高亮颜色
    Color menuHighlightColor;
    //
 
    void Awake()
    {
        //获取手柄上的这个组件
        trackdeObjec = GetComponent<SteamVR_TrackedObject>();
        //刀取消激活
        kinfe.SetActive(false);
        //武器列表取消激活
        weaponMenu.SetActive(false);
 
    }
 
    // Use this for initialization
    void Start () {
        //获取武器列表选项初始颜色
        Material orignMaterial = weaponMenuGun.GetComponent<Renderer>().material;
        menuNormalColor = orignMaterial.color;
        //制作高亮颜色
        menuHighlightColor = menuNormalColor + Color.grey;
    }
     
    // Update is called once per frame
    void Update () {
     
    }
    void FixedUpdate()
    {   //获取vive手柄输入
        var device = SteamVR_Controller.Input((int)trackdeObjec.index);
        //实时获取激光起点
        Vector3 rayStartPos = rayStart.position;
        //实时获取激光方向
        Vector3 rayDirdctionn = rayTarget.position;
        //激光方向
        Vector3 rayFinDirection = (rayDirdctionn - rayStartPos).normalized;
        //发射射线
        Ray ray = new Ray(rayStartPos, rayFinDirection);
        RaycastHit hitInfo = new RaycastHit();
        //打印射线
        Debug.DrawRay(ray.origin, ray.direction * 100, Color.green);
 
            if (device.GetTouch(SteamVR_Controller.ButtonMask.ApplicationMenu))
            {   //按下菜单键激活武器列表 
                weaponMenu.SetActive(true);
            //如果激光接触物体(列表)
            if (Physics.Raycast(ray, out hitInfo))
            {
                if (hitInfo.collider != null)
                {//射线选中了武器列表哪个选项的格子,哪个格子就会高亮,射线离开取消高亮恢复颜色;
                    if (hitInfo.collider.gameObject.tag == "gun")
                    {
                        weaponMenuGun.GetComponent<Renderer>().material.color = menuHighlightColor;
                    }
                    else
                    {
                        weaponMenuGun.GetComponent<Renderer>().material.color = menuNormalColor;
                    }
                    if (hitInfo.collider.gameObject.tag == "knife")
                    {
                        weaponMeunKnife.GetComponent<Renderer>().material.color = menuHighlightColor;
                    }
                    else
                    {
                        weaponMeunKnife.GetComponent<Renderer>().material.color = menuNormalColor;
                    }
                }
            }
               }
             
 
             //如果在枪这里松开了菜单按钮,就会激活枪,取消激活刀和菜单
                if (device.GetTouchUp(SteamVR_Controller.ButtonMask.ApplicationMenu))
        {
             
            //如果激光接触物体(列表)
            if (Physics.Raycast(ray, out hitInfo))
            {    //这里我把枪选项的格子tag设为了gun,刀选项的格子tag设为了knife
                if (hitInfo.collider.gameObject.tag == "gun")
                {
                    Debug.Log("选中了枪");
                    
                    gun.SetActive(true);
                    kinfe.SetActive(false);
                }
                if (hitInfo.collider.gameObject.tag == "knife")
                {   //道理同上
                   
                        Debug.Log("选中了刀");
                        
                        gun.SetActive(false);
                        kinfe.SetActive(true);
                     
                }
 
            }
            weaponMenu.SetActive(false);
        }
             
            
         
 
         
        }
    }

demo下载地址:

http://download.csdn.net/detail/ystistheking/9550100

htc vive 游戏切换武器功能思路以及demo(unity,c#)

标签:

原文地址:http://blog.csdn.net/ystistheking/article/details/51680362

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!