Stealth学习第一天

1.导入场景资源
2.给地形添加碰撞器,设计好场景地形
3.给场景添加灯光,照亮场景,自己设计适合的氛围灯光
4.给场景添加警报灯
-添加一个Directional light
-在Directional light上添加脚本,控制灯光的强弱,造成闪烁效果

using System.Collections.Generic;
using UnityEngine;
/*
这是一个控制警报灯开关的脚本
*/
public class AlertLigth : MonoBehaviour {
    //做成单例模式
    public static AlertLigth _instance;
    private Light lt;
    //警报灯的开关
    public bool isOn = false;
    private float initialLight = 0;
    private float terminusLight = 0.5f;
    //灯光的目标值
    private float targetLight;
    //灯光变化的速度
    public float speedLight = 3;

    // Use this for initialization
    void Awake () {
        targetLight = terminusLight;
        lt = GetComponent();
        isOn = false;
        _instance = this;
    }
    
    // Update is called once per frame
    void Update () {
        if(isOn) {
            lt.intensity = Mathf.Lerp(lt.intensity,targetLight,Time.deltaTime * speedLight);
            if(Mathf.Abs(lt.intensity - targetLight)  <= 0.05f){
                targetLight = initialLight;
            }else if(targetLight == initialLight) {
                targetLight = terminusLight;
            }
        }else {
            lt.intensity = Mathf.Lerp(lt.intensity, 0f, Time.deltaTime);
        }
    }
}

做成单例方便在其他脚本中获取该脚本 控制警报的开启关闭
5.选择游戏场景中的广播物体,为每个物体添加声音组件Audio Source
6.创建游戏控制器,通过查找声音组件的 tag获取到AudioSource的集合,遍历集合播放警报声音,同时控制警报灯闪烁.
代码如下

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

public class GameController : MonoBehaviour {
    public static GameController _intanens;

    public Vector3 lastPlayerPosition = Vector3.zero;
    public bool isOnOff = false;
    private GameObject[] siren;

    // Use this for initialization
    void Awake () {
        _intanens = this;
        isOnOff = false;
        siren = GameObject.FindGameObjectsWithTag(Tags.siren);
    }
    
    // Update is called once per frame
    void Update () {
        AlertLigth._instance.isOn = isOnOff;

        if(isOnOff) {
            OpenSiren();
        }else {
            StopSiren();
        }
    }

    //开启声音的方法
    void OpenSiren() {
        foreach(GameObject go in siren) {
            AudioSource myAudio = go.GetComponent();
            if(!myAudio.isPlaying) {
                myAudio.Play();
            }
        }
    }
    //关闭声音的方法
    void StopSiren() {
        foreach(GameObject go in siren) {
            AudioSource myAudio = go.GetComponent();
            myAudio.Stop();
         }
    }
}

到此由游戏控制器控制警报的声音灯光基本告一段落
7.添加红外探照灯,通过Animator组件制作片段Animation,添加摄像头旋转探测
8.将 tag进行统一管理

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

public class Tags : MonoBehaviour {

    public const string player = "Player";
    public const string siren = "Siren";

}

Day01的学习到此结束!

你可能感兴趣的:(Stealth学习第一天)