摄像机的安全检测

摄像机安全检测

安全监测的方式有很多种

请添加图片描述

2.当摄像机发射线可以碰到玩家时,且摄像机当前不在初始位置

如果上一个位置能看到家,则后退一步

如果看不到玩家,不动

public class cameraSafe : MonoBehaviour
{

    //在相机下定义一个空节点作为初始位置
    public Transform StartPos;
    //在相机下定义一个空节点作为结束位置
    public Transform EndPos;

    //插值的第三个参数
    public float t;

    // Start is called before the first frame update
    void Start()
    {
        t = 0;
        transform.LookAt(transform.parent);
    }

    // Update is called once per frame
    void Update()
    {
        Ray ray = new Ray(transform.position, (transform.parent.position - transform.position).normalized);
        RaycastHit hit;
        if (Physics.Raycast(ray, out hit))
        {
            if (hit.collider.gameObject.tag != "Player")
            {
                t += 0.25f;
                //钳制
                t = Mathf.Clamp(t, 0, 1);
                transform.position = Vector3.Slerp(StartPos.position, EndPos.position, t);
                transform.LookAt(transform.parent);
                GetComponent<Camera>().fieldOfView += 10;
            }
            //当摄像机发射线可以碰到玩家时,且摄像机当前不在初始位置
            else if (t != 0)
            {
                Vector3 beforePos = Vector3.Slerp(StartPos.position, EndPos.position, t - 0.25f);
                Ray ray2 = new Ray(beforePos,(transform.parent.position - beforePos).normalized );
                RaycastHit hit2;
                if(Physics.Raycast(ray2, out hit2))
                {
                    //如果上一个位置能看到家,则后退一步
                    //如果看不到玩家,不动
                    if (hit2.collider.gameObject.tag == "Player")
                    {
                        t -= 0.25f;
                        GetComponent<Camera>().fieldOfView += 10;
                        //钳制
                        t = Mathf.Clamp(t, 0, 1);
                        transform.position = Vector3.Slerp(StartPos.position, EndPos.position, t);
                        transform.LookAt(transform.parent);
                    }
                }

            }
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值