美文网首页VR专题
VR开发 -- HTC Vive实现瞬移功能

VR开发 -- HTC Vive实现瞬移功能

作者: Shawn_正品鞋铺 | 来源:发表于2017-03-06 23:27 被阅读1825次
    一.什么是瞬移功能?

    瞬移就是使用手柄选取一个位置,通过扳机键进行瞬间移动。
    因为HTC Vive活动范围有限制(3m * 5m),所以我们在实际的使用中,为了更好的浏览场景,查看场景,会使用这种移动的方式,达到游戏中的视野移动。
    移动的方式有两种:一种是水平移动即X,Z轴移动;另一种就是垂直移动,即Y轴移动。
    PS : 我们通过射线检测的方法进行开发,因为HTC Vive有类似的脚本。所以我们只需更改脚本的某些部分就可实现瞬移功能。

    二.案例详解
    1.下载Steam VR,导入资源
    Paste_Image.png
    2.将脚本复制,修改类名(部分脚本)
    Paste_Image.png
    using UnityEngine;
    using System.Collections;
    
    public class SteamVR_LaserPointer : MonoBehaviour
    {
    public bool active = true;
    public Color color;
    public float thickness = 0.002f;
    public GameObject holder;
    public GameObject pointer;
    bool isActive = false;
    public bool addRigidBody = false;
    public Transform reference;
    public event PointerEventHandler PointerIn;
    public event PointerEventHandler PointerOut;
    public Vector3 HitPoint;
    
    
    Transform previousContact = null;
    
    // Use this for initialization
    void Start ()
    {
        holder = new GameObject();
        holder.transform.parent = this.transform;
        holder.transform.localPosition = Vector3.zero;
        holder.transform.localRotation = Quaternion.identity;
    
        pointer = GameObject.CreatePrimitive(PrimitiveType.Cube);
        pointer.transform.parent = holder.transform;
        pointer.transform.localScale = new Vector3(thickness, thickness, 100f);
        pointer.transform.localPosition = new Vector3(0f, 0f, 50f);
        pointer.transform.localRotation = Quaternion.identity;
        BoxCollider collider = pointer.GetComponent<BoxCollider>();
        if (addRigidBody)
        {
            if (collider)
            {
                collider.isTrigger = true;
            }
            Rigidbody rigidBody = pointer.AddComponent<Rigidbody>();
            rigidBody.isKinematic = true;
        }
        else
        {
            if(collider)
            {
                Object.Destroy(collider);
            }
        }
        Material newMaterial = new Material(Shader.Find("Unlit/Color"));
        newMaterial.SetColor("_Color", color);
        pointer.GetComponent<MeshRenderer>().material = newMaterial;
    }
    
    public virtual void OnPointerIn(PointerEventArgs e)
    {
        if (PointerIn != null)
            PointerIn(this, e);
    }
    
    public virtual void OnPointerOut(PointerEventArgs e)
    {
        if (PointerOut != null)
            PointerOut(this, e);
    }
    
    
    // Update is called once per frame
    void Update ()
    {
        if (!isActive)
        {
            isActive = true;
            this.transform.GetChild(0).gameObject.SetActive(true);
        }
    
        float dist = 100f;
    
        SteamVR_TrackedController controller = GetComponent<SteamVR_TrackedController>();
    
        Ray raycast = new Ray(transform.position, transform.forward);
        RaycastHit hit;
        bool bHit = Physics.Raycast(raycast, out hit);
    
        if(previousContact && previousContact != hit.transform)
        {
            PointerEventArgs args = new PointerEventArgs();
            if (controller != null)
            {
                args.controllerIndex = controller.controllerIndex;
            }
            args.distance = 0f;
            args.flags = 0;
            args.target = previousContact;
            OnPointerOut(args);
            previousContact = null;
        }
        if(bHit && previousContact != hit.transform)
        {
            PointerEventArgs argsIn = new PointerEventArgs();
            if (controller != null)
            {
                argsIn.controllerIndex = controller.controllerIndex;
            }
            argsIn.distance = hit.distance;
            argsIn.flags = 0;
            argsIn.target = hit.transform;
            OnPointerIn(argsIn);
            previousContact = hit.transform;
        }
        if(!bHit)
        {
            previousContact = null;
        }
        if (bHit)
        {
            HitPoint = hit.point;
        }
        if (bHit && hit.distance < 100f)
        {
            dist = hit.distance;
        }
    
        if (controller != null && controller.triggerPressed)
        {
            pointer.transform.localScale = new Vector3(thickness * 5f, thickness * 5f, dist);
        }
        else
        {
            pointer.transform.localScale = new Vector3(thickness, thickness, dist);
        }
        pointer.transform.localPosition = new Vector3(0f, 0f, dist/2f);
       }
    }
    
    3.添加脚本
    Paste_Image.png Paste_Image.png
    4.添加传送脚本
    public class Test_TelePointer : MonoBehaviour {
    
    //拿到手柄对象
    public GameObject LeftController;
    //修改后的射线检查脚本
    private Test_LaserPointer Pointer;
    //右手柄的控制器
    private SteamVR_TrackedController LeftSt;
    //事件调用函数
    ClickedEventHandler ce;
    //当前的目标位置
    Transform currentTransform;
        //碰撞事件
    PointerEventArgs arg;
    
    void Start () {
        Pointer = LeftController.GetComponent<Test_LaserPointer>();
        LeftSt = LeftController.GetComponent<SteamVR_TrackedController>();
        //利用Laserpoint里面的事件函数来检测目标位置
        Pointer.PointerIn += LeftPointIn;
        //利用Laserpoint里面的事件函数来检测目标位置
        Pointer.PointerOut += LeftPointOut;
        //利用手柄控制器来检测我们的扳机事件
        LeftSt.TriggerClicked += TriggerClicked;
    }
    
    
    void Update () {
    
    }
    
    //没有检测到碰撞信息
    void LeftPointOut(object sender, PointerEventArgs e)
    {
        //当前位置为空
        currentTransform = null;
    }
    //检测到碰撞信息
    void LeftPointIn(object sender, PointerEventArgs e)
    {   
        // 设置目标位置为检测到的目标
        currentTransform = e.target;
    }
    //扳机事件
    void TriggerClicked(object sender, ClickedEventArgs e)
    {
        //如果检测到目标位置
        if (currentTransform != null)
        {
            //移动
            this.gameObject.transform.position = new Vector3(Pointer.HitPoint.x - LeftController.transform.localPosition.x, Pointer.HitPoint.y, Pointer.HitPoint.z - LeftController.transform.localPosition.z);
        }  
    }
    

    }

    相关文章

      网友评论

        本文标题:VR开发 -- HTC Vive实现瞬移功能

        本文链接:https://www.haomeiwen.com/subject/xfkpgttx.html