|  | 
 
| 设置游戏中的警告监控器,使用监控器的模型创建巡视动画。这段教程中讲解了如何创建监视器、如何创建监视器的摆头动画,如何创建监视器的视野以及发射的红色激光束。 监视器的代码相对比较简单,即当玩家进入摄像机视野就触发警报,然后将全局的一个焦点聚焦到玩家上。
 CCTVPlayerDetection
 C#脚本
 JS脚本复制代码using UnityEngine;
using System.Collections;
public class CCTVPlayerDetection : MonoBehaviour
{
    private GameObject player;                          // 指向玩家的变量.
    private LastPlayerSighting lastPlayerSighting;      // 指向玩家的视野的全局变量.
    
    void Awake ()
    {
        // 设置变量.
        player = GameObject.FindGameObjectWithTag(Tags.player);
        lastPlayerSighting = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<LastPlayerSighting>();
    }
    
    
    void OnTriggerStay (Collider other)
    {
        // 如果闯进碰撞检测区域的玩家...
        if(other.gameObject == player)
        {
            // ... 从摄像机发射一个射线射向玩家.
            Vector3 relPlayerPos = player.transform.position - transform.position;
            RaycastHit hit;
            
            if(Physics.Raycast(transform.position, relPlayerPos, out hit))
                // 如果射线碰上了玩家...
                if(hit.collider.gameObject == player)
                    // ... 将聚焦点设在玩家身上.
                    lastPlayerSighting.position = player.transform.position;
        }
    }
}
相关知识:Light probes  灯光探测器复制代码#pragma strict
private var player : GameObject;                                // Reference to the player.
private var lastPlayerSighting : LastPlayerSighting;        // Reference to the global last sighting of the player.
function Awake ()
{
    // Setting up the references.
    player = GameObject.FindGameObjectWithTag(Tags.player);
    lastPlayerSighting = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent(LastPlayerSighting);
}
function OnTriggerStay (other : Collider)
{
    // If the colliding gameobject is the player...
    if(other.gameObject == player)
    {
        // ... raycast from the camera towards the player.
        var relPlayerPos : Vector3 = player.transform.position - transform.position;
        var hit : RaycastHit;
        
        if(Physics.Raycast(transform.position, relPlayerPos, hit))
            // If the raycast hits the player...
            if(hit.collider.gameObject == player)
                // ... set the last global sighting of the player to the player's position.
                lastPlayerSighting.position = player.transform.position;
    }
}
 | 
 |