此脚本挂到相机上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GazeController : MonoBehaviour
{
//准星容器
public Canvas reticleCanvas;
//准星
public Image reticleImage;
//击中的当前目标
public GameObject target;
//初始位置
private Vector3 originPos;
//初始缩放
private Vector3 originScale;
//倒计时时间
private float countDownTime = 3;
//当前时间
private float currentTime = 0;
// Use this for initialization
void Start()
{
reticleImage.fillAmount = 0;
//记录初始位置
originPos = reticleCanvas.transform.localPosition;
//记录初始缩放
originScale = reticleCanvas.transform.localScale;
}
// Update is called once per frame
void Update()
{
Ray ray = new Ray(transform.position, transform.forward);
RaycastHit hit;
//如果碰撞到了物体
if (Physics.Raycast(ray, out hit, 50))
{
//将碰撞的位置赋给准星
reticleCanvas.transform.position = hit.point;
//根据距离进行缩放,补偿在3d世界中近大远小的情况
reticleCanvas.transform.localScale = originScale * hit.distance;
//让准星与碰撞的物体垂直通过让准星与击中点法线方向一致
reticleCanvas.transform.forward = hit.normal;
//视线初次进入
if (hit.transform.gameObject != target)
{
//如果上次的目标物体不为空,进行移出的操作
if (target != null)
{
VRGazeItem oldItem = target.GetComponent<VRGazeItem>();
if (oldItem)
{
oldItem.GazeOut();
}
}
//将击中的目标赋给当前的目标物体
target = hit.transform.gameObject;
//获取物体上的凝视组件
VRGazeItem newItem = target.GetComponent<VRGazeItem>();
//如果有凝视组件
if (newItem)
{
//凝视
newItem.GazeIn();
}
}
else//视线在此停留
{
currentTime += Time.deltaTime;
//设定时间未结束
if (countDownTime - currentTime > 0)
{
//设置进度条
reticleImage.fillAmount = currentTime / countDownTime;
}
else//达到设定条件
{
currentTime = 0;
//如果
VRGazeItem gazeFireItem = target.GetComponent<VRGazeItem>();
if (gazeFireItem)
{
gazeFireItem.GazeFire(hit);
}
}
}
}
//没有碰撞到物体
else
{
reticleCanvas.transform.position = originPos;
//缩放复位
reticleCanvas.transform.localScale = originScale;
//准星方向复位
reticleCanvas.transform.forward = Camera.main.transform.forward;
reticleImage.fillAmount = 0;
}
}
}
此脚本挂到 被凝视的物体上
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class VRGazeItem : MonoBehaviour
{
//高亮材质
public Material highlightMat;
//正常材质
public Material normalMat;
public Image img;
private bool gggg;
public Image img2;
public GameObject building10;
private
void Start()
{
}
void Update()
{
}
//视线移入处理函数
public void GazeIn()
{
if (gameObject.tag == "GazeUI")
{
//鼠标移入的效果
ExecuteEvents.Execute(gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.pointerEnterHandler);
StopAllCoroutines();
}
else if (gameObject.tag == "GazeObj")
{
//给cube添加高亮时变成蓝色
gameObject.GetComponent<Renderer>().material = highlightMat;
}
}
//视线移出处理函数
public void GazeOut()
{
if (gameObject.tag == "GazeUI")
{
//鼠标移入的效果
ExecuteEvents.Execute(gameObject, new PointerEventData(EventSystem.current), ExecuteEvents.pointerExitHandler);
}
else if (gameObject.tag == "GazeObj")
{
//cube变成正常的颜色
gameObject.GetComponent<Renderer>().material = normalMat;
}
}
//视线激活处理函数
public void GazeFire(RaycastHit hit)
{
if (hit.transform.name == "转换场景")
{
gggg = true;
}
if (gameObject.tag == "GazeUI")
{
//隐藏物体
//gameObject.SetActive(false);
StartCoroutine(StartUI());
}
else if (gameObject.tag == "GazeObj")
{
//给物体cube作用一个力
gameObject.GetComponent<Rigidbody>().AddForceAtPosition(hit.point.normalized * 100, hit.point);
}
}
IEnumerator StartUI() {
img.GetComponent<Image>().fillAmount += Time.deltaTime;
Debug.Log("zz正在加载");
yield return new WaitForSeconds(Time.deltaTime);
yield return StartUI2();
}
IEnumerator StartUI2()
{
if (img.GetComponent<Image>().fillAmount<1)
{
yield return StartUI();
}
else
{
building10.SetActive(true);
img.GetComponent<Image>().fillAmount = 0;
// img2.gameObject.SetActive(false);
gameObject.SetActive(false);
if (gggg == true)
{
SceneManager.LoadScene(0);
}
}
}
}