用Unity開發HTC VIVE——移動漫遊篇

來源:互聯網
上載者:User

標籤:speed   pad   component   float   ini   frame   base   returns   sam   

這篇文章主要寫的是通過手柄控制移動在情境中漫遊。
在通過手柄控制移動時,我主要寫了兩個指令碼一個ChildTransform.cs、Move.cs;
1、 ChildTransform這個指令碼主要是為了擷取頭部Y軸方向的轉動。以及頭部在x、z軸方向的移動。將這個資訊賦值給這個指令碼綁定的對象身上。
2、 Move這個指令碼主要是為了控制玩家的移動的,移動的方向是依據綁定ChildTransform這個指令碼的transform資訊。
這樣就能實現,頭盔轉動控制移動的方向,手柄中touchPad的上、下、左、右控制移動的向前、向左、向右、向後移動。
操作步驟:
1、 首先我們需要先建立一個Null 物件,命名為moveDic。
2、 然後將指令碼ChildTransform綁定在moveDic上。
3、 將Camera(head)賦值給ChildTransform的Same變數。
這個就能將Camera(head)的方向資訊賦值給moveDic。
指令碼
Movie.cs 有3個公開變數:
1、 Player:將 [Camera Rig]賦值給它。
2、 Dic:將moveDic賦值給它。

3、 Speed:主要是控制移動的速度。

 

指令碼ChildTransform.cs:

using UnityEngine;using System.Collections;public class ChildTransform : MonoBehaviour{    public Transform same;// Use this for initializationvoid Start () {}// Update is called once per framevoid FixedUpdate () {        transform.localEulerAngles = new Vector3(0,same.localEulerAngles.y,0);        transform.localPosition = new Vector3(same.localPosition.x, 0, same.localPosition.z);}}

 

 

指令碼Move.cs:

 

using UnityEngine;using System.Collections;public class Move : BaseClass{    /// <summary>    /// 手柄位置    /// </summary>    SteamVR_TrackedObject tracked;    /// <summary>    /// 玩家    /// </summary>    public Transform player;    /// <summary>    /// 方向     /// </summary>    public Transform dic;    /// <summary>    /// 速度    /// </summary>    public float speed;    void Awake()    {        //擷取手柄控制        tracked = GetComponent<SteamVR_TrackedObject>();    }// Use this for initializationvoid Start () {}// Update is called once per framevoid FixedUpdate () {        var deviceright = SteamVR_Controller.Input((int)tracked.index);        //按下圓盤鍵        if (deviceright.GetPress(SteamVR_Controller.ButtonMask.Touchpad))        {            Vector2 cc = deviceright.GetAxis();            float angle = VectorAngle(new Vector2(1, 0), cc);            //下            if (angle > 45 && angle < 135)            {                player.Translate(-dic.forward * Time.deltaTime * speed);            }            //上              else if (angle < -45 && angle > -135)            {                //Debug.Log("上");                player.Translate(dic.forward * Time.deltaTime * speed);            }            //左              else if ((angle < 180 && angle > 135) || (angle < -135 && angle > -180))            {                //Debug.Log("左");                player.Translate(-dic.right * Time.deltaTime * speed);            }            //右              else if ((angle > 0 && angle < 45) || (angle > -45 && angle < 0))            {                //Debug.Log("右");                player.Translate(dic.right * Time.deltaTime * speed);            }        }}    /// <summary>    /// 根據在圓盤才按下的位置,返回一個角度值    /// </summary>    /// <param name="from"></param>    /// <param name="to"></param>    /// <returns></returns>    float VectorAngle(Vector2 from, Vector2 to)    {        float angle;        Vector3 cross = Vector3.Cross(from, to);        angle = Vector2.Angle(from, to);        return cross.z > 0 ? -angle : angle;    }}

Move指令碼主要是根據

deviceright.GetAxis()擷取在TouchPad中按下的位置資訊,然後與(0,1)點求夾角。然後根據這個角度來判斷在按下是TouchPad的上、下、左、右。
其實TouchPad就相當於一個半徑為1的圓(單位圓)。而<span style="font-family: Arial, Helvetica, sans-serif;">GetAxis(),就是單位圓中點的座標。</span>
轉載自:http://www.52vr.com/article-390-1.html

用Unity開發HTC VIVE——移動漫遊篇

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.