Unity入门项目John Lemon‘s Haunted Jaunt中的Obsever脚本

项目文档链接:John Lemon's Haunted Jaunt 项目教程 (适用于3D初学者) - Unity Learn

总代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Observer : MonoBehaviour
{
    public Transform player;
    public GameEnding gameEnding;
    bool m_IsPlayInRange;
     void OnTriggerEnter(Collider other)
    {
        if (other.transform == player)
        {
            m_IsPlayInRange = true;
        }
    }
    void OnTriggerExit(Collider other)
    {
        if (other.transform == player)
        {
            m_IsPlayInRange = false;
        }
    }
    private void Update()
    {
        if (m_IsPlayInRange)
        {
            Vector3 direction = player.position - transform.position + Vector3.up;
            Ray ray = new Ray(transform.position, direction);
            RaycastHit raycastHit;
            if (Physics.Raycast(ray,out raycastHit))
            {
                if(raycastHit.collider.transform == player) {
                    gameEnding.CaughtPlayer();
                }
            }
        }
    }
}

---------------------------------------------------------------------------------------------------------------------------------

Tramsform是Unity的一个组件,是每个游戏对象的跟节点,包含了对象在3D空间的位置、旋转等在C#中使用Unity.Engine即可使用其方法

Verctor3.up是一个静态只读的数下,表示一个向上的方向向量

Ray是Unity.Engine中的一个类 用于表示射线

new一个ray的时候第一个参数是起点 第二个是终点

RaycastHit 是Unity中的结构体,用于存储射线(Ray)的碰撞信息。在本项目中用于制作怪物的视野碰撞。

Physics.Raycast是Unity中用于检测射线是否与场景中的物体发生碰撞的方法,定义在UnityEngine.Physics类中,其第一个参数为射线、第二个参数是用于存储碰撞信息的RaycastHit结构体

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值