VR開發(fā)實戰(zhàn)CardBoard項目之空中對壘

一、場景

1,天空盒
2,主角-坦克
3,敵人-進攻的直升機

Target

using UnityEngine;
using System.Collections;
//此腳本掛在直升機上
[RequireComponent(typeof(Collider))]
public class Target : MonoBehaviour,IGvrGazeResponder {

    private const float IntervalTime = 2;//攻擊間隔
    private float realInterval = 0;
    public GameObject m_LaserCannon_Prefab;
    public GameObject explosion;

    private void shootPlayer()
    {
        if (realInterval < IntervalTime)
        {
            realInterval += Time.deltaTime;
            return;
        }
        realInterval = 0;
        Transform player = Camera.main.transform;
        Transform start = this.transform;
        Quaternion goal = Quaternion.FromToRotation(Vector3.zero, -player.position +start.position);
        
        GameObject go = GameObject.Instantiate(m_LaserCannon_Prefab, start.position,goal) as GameObject;
        GameObject.Destroy(go, 5f);
    }

    // Use this for initialization
    void Start () {
        SetGazedAt(false);
    }

    void LateUpdate()
    {
        GvrViewer.Instance.UpdateState();
        if (GvrViewer.Instance.BackButtonPressed)
        {
            Application.Quit();
        }
    }
    // Update is called once per frame
    void Update () {
        shootPlayer();
    }

    public void SetGazedAt(bool gazedAt)
    {
       //可以實現(xiàn)一些計時,或賦于盯更多意義等的操作
    }

    #region IGvrGazeResponder implementation

    /// Called when the user is looking on a GameObject with this script,
    /// as long as it is set to an appropriate layer (see GvrGaze).
    public void OnGazeEnter()
    {
        Debug.Log("in");
        SetGazedAt(true);
    }

    /// Called when the user stops looking on the GameObject, after OnGazeEnter
    /// was already called.
    public void OnGazeExit()
    {
        SetGazedAt(false);
    }

    /// Called when the viewer's trigger is used, between OnGazeEnter and OnGazeExit.
    public void OnGazeTrigger()
    {
        //TeleportRandomly();
    }

    #endregion


    void OnTriggerEnter(Collider e)
    {
        if (e.gameObject.tag.CompareTo("tagDestroyBall") == 0)
        {
            GvrAudioSource audio = this.gameObject.GetComponent<GvrAudioSource>();
            audio.PlayOneShot(audio.clip);
            GameObject go = GameObject.Instantiate(explosion) as GameObject;
            go.transform.position = this.transform.position;
            go.transform.rotation = this.transform.rotation;
            GameObject.Destroy(go, 1.5f);
            GameObject.Destroy(this.gameObject, 1.5f);
            Destroy(e.gameObject);//銷毀加速球  
        }
    }  
}

4、炮彈和爆炸效果

敵方炮彈


ShotBehavior

using UnityEngine;
using System.Collections;

//此腳本掛在預(yù)制體shot_prefabs
public class ShotBehavior : MonoBehaviour {

    private Vector3 target ;

    // Use this for initialization
    void Start () {
        Transform player = Camera.main.transform;

        this.gameObject.transform.LookAt(player);
        Transform start = this.transform;
        target = player.position - start.position;
        target = target + new Vector3(0, -0.3f);
    }
    
    // Update is called once per frame
    void Update () {
        transform.position += target * Time.deltaTime * 1f;
    
    }
}

主角炮彈

using UnityEngine;
using System.Collections;

//此腳本掛在預(yù)制體shot_prefabs
public class ShotBehavior : MonoBehaviour {

    private Vector3 target ;

    // Use this for initialization
    void Start () {
        Transform player = Camera.main.transform;

        this.gameObject.transform.LookAt(player);
        Transform start = this.transform;
        target = player.position - start.position;
        target = target + new Vector3(0, -0.3f);
    }
    
    // Update is called once per frame
    void Update () {
        transform.position += target * Time.deltaTime * 1f;
    
    }
}


CannonBehavior

using UnityEngine;
using System.Collections;

public class CannonBehavior : MonoBehaviour {

    public Transform m_cannonRot;
    public Transform m_muzzle;
    public GameObject m_shotPrefab;

    // Use this for initialization
    void Start () 
    {
    
    }
    
    // Update is called once per frame
    void Update () 
    {
        if (Input.GetKeyDown(KeyCode.Space)||(Input.GetMouseButtonDown(0)))
        {
            Shoot();
        }
    }
    public void Shoot()
    {
        GameObject go = GameObject.Instantiate(m_shotPrefab, m_muzzle.position, m_muzzle.rotation) as GameObject;
        GameObject.Destroy(go, 5f);
    }

}

二、交互

1、盯
2、單擊

三、展示效果

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 《紅海行動》上映全家都想看,第一遍看時錯誤的帶了孩子,觀影二十多分鐘孩子就被嚇得哭哭啼啼,于是只好帶著孩子離開影院...
    披塵掠風(fēng)閱讀 3,724評論 50 76
  • 廖瀚城本以為爆破山體炸下的石頭可以阻止敵人的裝甲部隊推進,但他低估了敵人工兵的力量,兩個個Y軍山地師已經(jīng)列...
    天火出擊閱讀 585評論 3 4
  • 貝加爾湖 穿上厚厚的防輻射服,我和其他幾個記者上了開往貝加爾湖戰(zhàn)區(qū)的直升機。離戰(zhàn)爭...
    六頭豬閱讀 636評論 0 1
  • 轉(zhuǎn)載——韓文軒 2004年11月,美國陸軍官員對以阿拉克戰(zhàn)場的UAV(無人戰(zhàn)斗機)進行了一次調(diào)查,結(jié)果顯示,各級指...
    卡瓦斯豬閱讀 2,459評論 0 7
  • 異星歧途(The Road Not Taken) 哈利·托特達夫(Harry Turtledove) 姚人杰 ...
    ChrisAnderson閱讀 72,662評論 3 106

友情鏈接更多精彩內(nèi)容