一、场景
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)
{
//可以实现一些计时,或赋于盯更多意义等的操作
}
#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;
//此脚本挂在预制体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;
//此脚本挂在预制体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);
}
}