UnityVR游乐园笔记#003
跟着课程做的一个项目(VR游乐园)
1. 摄像机跟随过山车的座位进行移动
用代码让摄像机跟随
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test : MonoBehaviour
{
public Transform pos;
public Transform rot;
private void Awake()
{
UnityEngine.XR.InputTracking.disablePositionalTracking = false;
}
private void Update()
{
transform.position = pos.position + Vector3.up * 0.8f;
transform.eulerAngles = new Vector3(rot.eulerAngles.x, rot.eulerAngles.y, rot.eulerAngles.z + 100f);
transform.localScale = Vector3.one;
}
}
2. 添加到终点倒计时
车身上的脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RollerCoaster : MonoBehaviour
{
public AudioSource aud;
private Animation ani;
private float aniTime;
private float timer;
private bool isPlay = false;
private bool isEnd = false;
private void Awake()
{
ani = GetComponent<Animation>();
aniTime = ani.clip.length;
}
private void Update()
{
aniTime -= Time.deltaTime;
if(aniTime <= 10 && !isPlay)
{
aud.Play();
isPlay = true;
}
if(aniTime <= 0 && !isEnd)
{
UnityEngine.SceneManagement.SceneManager.LoadScene("MainScene");
isEnd = true;
}
if(aniTime <= 4)
{
timer += Time.deltaTime;
if(timer >= 1)
{
timer = 0;
CountDown.instance.OnCountDown();
}
}
}
}
Canvas身上的脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CountDown : MonoBehaviour
{
public static CountDown instance;
private Text text;
private int timer = 3;
private void Awake()
{
instance = this;
text = transform.Find("CountDown").GetComponent<Text>();
gameObject.SetActive(false);
}
public void OnCountDown()
{
if(timer > 0)
{
gameObject.SetActive(true);
text.text = timer.ToString();
timer--;
}
}
}