Unity笔记 Stealth 实例详解

来源:互联网 发布:java中1到100的质数 编辑:程序博客网 时间:2024/06/18 06:10

第一部分
http://unity3d.com/learn/tutorials/projects/stealth-tutorial-4x-only

1 介绍
一个3D项目,初、中级难度

2 添加环境模型、光照贴图lightmap
lightmapping:
设置static模型,才可以使用lightmap。
extents选layer->extent。 别的选在layer-> playarea。
打开fog 调整quality
bake lightmapping:
先bake low quality,pad选择2。
场景中的main和alarm两个光设置为realtime不参与bake lightmapping

3 警报灯
用脚本控制警报的intensity,制作一闪一闪(pulse)的效果。
添加了一个音原到扩音器(megaphone)

4 管理tag
一些全局静态字符串,方便以后在代码各处使用。

public class Tags : MonoBehaviour{    // A list of tag strings.    public const string player = "Player";    public const string alarm = "AlarmLight";    public const string siren = "Siren";    public const string gameController = "GameController";    public const string mainLight = "MainLight";    public const string fader = "Fader";    public const string enemy = "Enemy";}

5 屏幕fade效果
用一张黑色图片控制alpha值来全屏遮挡。
build时,添加场景到build list,会自动递增一个index给这个scene。

6 GameController
GameController管理游戏内通用的一些行为(目前是播放音乐和角色位置管理)。
建立一个GameController空物体,可以加normalmusic音源和insight脚本检测玩家被发现时的位置。
加入panic音源做为子物体,做为被发现时的背景音。

7 闭路电视
加入模型,勾选use light probes。加入个截头体模型做碰撞用。
可视化用spotlight发出红色的光,并设置realtimeonly,设置cookie(投影纹理)。
添加脚本,检查玩家是不是被看到。
设置insight脚本成员:
lastPlayerSighting = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent();
添加cctvsweep动画,来移动摄像机。在animation窗口中选好时间后点击添加关键帧。选择pingpong插值方式,这样可以只用2帧来建立一个循环动画。

8 激光
激光门:
添加模型,添加碰撞体,点击istrigger。
添加自发光,类似cctv也是realtimeonly。
添加blinking脚本,来触发insight改变场景状态。

激光门开关:
添加2个碰撞体,一个用于避免重合,另外一个触发开关。

scripts可以添加子文件夹,便于组织脚本。

第二部分
1 建立player
导入模型,受到light probes影响。
添加胶囊碰撞体。
在rigidbody 中 freeze rotation x y z(避免物理旋转) 和 position y(避免y方向位移)
移除camera上的listener,并在player上添加audio listener。

2 Player AC
float paramater的使用,speed。
bool paramater的使用, sneaking dead shouting。
建立localmotion blendtree。添加2个motion,walk和run。取消automatic thresholds,选择speed来计算thresholds。
建立从idle到localmotion的transtion,设置condition,speed greater 0.1, sneaking false。
添加sneaking,做transtion和条件。
anystate的使用,可以用它来做die。
新建avatar mask(独立文件)来叠加shout,在ac中新建layer:shouting,选择mask,这样就可以在脚本中调用这个layer了,建立emptystate 和 shout 2个状态,shout in transtion是通过shouting这个bool,shout out transtion是默认的exit time。

3 hash id
建立一个ac的string to hashid的脚本,这样高效且不容易出现字符串拼写失误。

using UnityEngine;using System.Collections;public class HashIDs : MonoBehaviour{    // Here we store the hash tags for various strings used in our animators.    public int dyingState;    public int locomotionState;    public int shoutState;    public int deadBool;    public int speedFloat;    public int sneakingBool;    public int shoutingBool;    public int playerInSightBool;    public int shotFloat;    public int aimWeightFloat;    public int angularSpeedFloat;    public int openBool;    void Awake ()    {        dyingState = Animator.StringToHash("Base Layer.Dying");        locomotionState = Animator.StringToHash("Base Layer.Locomotion");        shoutState = Animator.StringToHash("Shouting.Shout");        deadBool = Animator.StringToHash("Dead");        speedFloat = Animator.StringToHash("Speed");        sneakingBool = Animator.StringToHash("Sneaking");        shoutingBool = Animator.StringToHash("Shouting");        playerInSightBool = Animator.StringToHash("PlayerInSight");        shotFloat = Animator.StringToHash("Shot");        aimWeightFloat = Animator.StringToHash("AimWeight");        angularSpeedFloat = Animator.StringToHash("AngularSpeed");        openBool = Animator.StringToHash("Open");    }}

4 player移动
设置ac layer的权 anim.SetLayerWeight(1, 1f); shouting layer index is 1
旋转player

void Rotating (float horizontal, float vertical)    {        // Create a new vector of the horizontal and vertical inputs.        Vector3 targetDirection = new Vector3(horizontal, 0f, vertical);        // Create a rotation based on this new vector assuming that up is the global y axis.        Quaternion targetRotation = Quaternion.LookRotation(targetDirection, Vector3.up);        // Create a rotation that is an increment closer to the target rotation from the player's rotation.        Quaternion newRotation = Quaternion.Lerp(rigidbody.rotation, targetRotation, turnSmoothing * Time.deltaTime);        // Change the players rotation to this new rotation.        rigidbody.MoveRotation(newRotation);    }

通过speed和AC机制来控制播放动画,动画中的位移会被应用到player。
通过AC调整动画播放速度。

5 Player 血量
集合了很多要用到的东西,在死的时候调用

 private Animator anim;                              // Reference to the animator component.    private PlayerMovement playerMovement;              // Reference to the player movement script.    private HashIDs hash;                               // Reference to the HashIDs.    private SceneFadeInOut sceneFadeInOut;              // Reference to the SceneFadeInOut script.    private LastPlayerSighting lastPlayerSighting;      // Reference to the LastPlayerSighting script.    private float timer;                                // A timer for counting to the reset of the level once the player is dead.    private bool playerDead;                            // A bool to show if the player is dead or not.

用一个bool来控制dying只调用一次,dead在update中反复调用。

第3部分
1 camera
在awake中建立一些camera的relative位置信息,这些是在编辑模式编辑的。
建立一个标准摄像机和一个高位摄像机(above),并在这2个摄像机中插值3个。用这5个摄像机逐个检测,发现有合适的就采用。例如靠墙站立,摄像机就会选择高位。

2 钥匙
inventory script加在玩家身上来看玩家是不是拥有key,
keypickup script在设置inventory有key后Destroy(gameObject);来摧毁自己。
在mesh render点选use light probes

0 0
原创粉丝点击