using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class shexian : MonoBehaviour
{
private LineRenderer line;
//注视时出现的动态圆环
public GameObject circle;
string objetname;
string newname;
string overName;
float angle_origin;
// Start is called before the first frame update
void Start()
{
line = Camera.main.GetComponent<LineRenderer>();
}
// Update is called once per frame
void Update()
{
//设置方向以及起始点
Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
//只可在Scene场景下看到
Debug.DrawLine(ray.origin, ray.direction);
RaycastHit hit;
//若没有看到物体
if (Physics.Raycast(ray, out hit)==false)
{
//设置Line的起始点、终点以及线的宽度
line.SetPosition(0, ray.origin + new Vector3(0, -1, 0));
line.SetPosition(1, ray.direction);
line.startWidth = 0.01f;
line.endWidth = 0f;
}
else if (Physics.Raycast(ray, out hit))
{
print(hit.collider.name);
line.startWidth = 0.01f;
line.endWidth = 0.0001f;
line.SetPosition(0, Camera.main.transform.position + new Vector3(0, -1, 0));
line.SetPosition(1, hit.collider.transform.position);
//聚焦点击事件
//动态圆环位置等于hit的位置
circle.transform.position = hit.transform.position;
//使用newname获取此时射线对照物体名字
newname = hit.collider.gameObject.name;
//新看向物体
if (overName == null)
{
overName = newname;//overname第三方存储器,用于和之后名字对比
}
else if (overName != null)//说明之前看到过按钮
{
if (overName == newname)//说明一直在看着之前的按钮
{
//显示射线
line.enabled = true;
//圆形指示板转动
angle_origin += Time.deltaTime * 0.5f;
circle.GetComponent<Image>().fillAmount = angle_origin;
if (angle_origin >= 1)
{
//圆环值为1,可触发事件
angle_origin = 0;
circle.GetComponent<Image>().fillAmount = angle_origin;
overName = null;
}
}
else//若现在名字不等于第三方存储器 说明中途看向了其他按钮
{
overName = null;
angle_origin = 0;
circle.GetComponent<Image>().fillAmount = angle_origin;
}
}
}
}
}
若射线颜色需要修改,把line组件下的材质球模式修改为Sprites/Default