【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 1631|回复: 0

6-5 The Lift 电梯设定

[复制链接]
发表于 2013-10-2 08:29:24 | 显示全部楼层 |阅读模式
如何控制一台电梯的逻辑LiftTrigger
C#
  1. using UnityEngine;
  2. using System.Collections;

  3. public class LiftTrigger : MonoBehaviour
  4. {
  5.         public float timeToDoorsClose = 2f;             // 从玩家进入电梯到电梯门关闭的时长.
  6.         public float timeToLiftStart = 3f;              // 从玩家进入电梯到电梯自动升高的时长.
  7.         public float timeToEndLevel = 6f;               // 游戏重新开始的时长间隔.
  8.         public float liftSpeed = 3f;                    // 电梯的升速.
  9.         
  10.         
  11.         private GameObject player;                      // Reference to the player.
  12.         private Animator playerAnim;                    // Reference to the players animator component.
  13.         private HashIDs hash;                           // Reference to the HashIDs script.
  14.         private CameraMovement camMovement;             // Reference to the camera movement script.
  15.         private SceneFadeInOut sceneFadeInOut;          // Reference to the SceneFadeInOut script.
  16.         private LiftDoorsTracking liftDoorsTracking;    // Reference to LiftDoorsTracking script.
  17.         private bool playerInLift;                      // 玩家是否在电梯内的标志位.
  18.         private float timer;                            // 时间计时器.


  19.         void Awake ()
  20.         {
  21.                 // 初始化变量.
  22.                 player = GameObject.FindGameObjectWithTag(Tags.player);
  23.                 playerAnim = player.GetComponent<Animator>();
  24.                 hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent<HashIDs>();
  25.                 camMovement = Camera.main.gameObject.GetComponent<CameraMovement>();
  26.                 sceneFadeInOut = GameObject.FindGameObjectWithTag(Tags.fader).GetComponent<SceneFadeInOut>();
  27.                 liftDoorsTracking = GetComponent<LiftDoorsTracking>();
  28.         }

  29.         
  30.         void OnTriggerEnter (Collider other)
  31.         {
  32.                 // 如果玩家进入电梯...
  33.                 if(other.gameObject == player)
  34.                         // ... 设置playerInLift标志位为true.
  35.                         playerInLift = true;
  36.         }

  37.         void OnTriggerExit (Collider other)
  38.         {
  39.                 // 如果玩家离开电梯...
  40.                 if(other.gameObject == player)
  41.                 {
  42.                         // ... 设置playerInLift标志位为false,并且重置计时器.
  43.                         playerInLift = false;
  44.                         timer = 0;
  45.                 }
  46.         }

  47.         //在这个函数中,处理电梯栅栏门的开关状态
  48.         //如果玩家还没有进入电梯或者进入电梯后还没有超过电梯门自动关闭时间,这个时候的栅栏门是跟随者外面红色铁皮门的开关而开关
  49.         //如果超过了自动关闭时间 电梯门就会自己关闭,将玩家关在电梯内部
  50.         void Update ()
  51.         {
  52.                 // 如果玩家是否进入到电梯内...
  53.                 if(playerInLift)
  54.                         // ... 执行逃生程序.
  55.                         LiftActivation();
  56.                
  57.                 // 如果计时器现在记录的时长小于关闭时间...
  58.                 if(timer < timeToDoorsClose)
  59.                         // ...电梯的栅栏门跟随者外面的铁皮红门的位置移动.
  60.                         liftDoorsTracking.DoorFollowing();
  61.                 else
  62.                         // 电梯的栅栏门自行关闭.
  63.                         liftDoorsTracking.CloseDoors();
  64.         }

  65.         void LiftActivation ()
  66.         {
  67.                 // 时间计数器增加.
  68.                 timer += Time.deltaTime;
  69.                
  70.                 // 时间计数器大于了电梯自动升起时间...
  71.                 if(timer >= timeToLiftStart)
  72.                 {o
  73.                         // ... 玩家的移动速度为0,摄像机控制失效,玩家变成电梯的子物体,跟随电梯移动.
  74.                         playerAnim.SetFloat(hash.speedFloat,0f);
  75.                         camMovement.enabled = false;
  76.                         player.transform.parent = transform;
  77.                         
  78.                         // 电梯向上升起.
  79.                         transform.Translate(Vector3.up * liftSpeed * Time.deltaTime);
  80.                         
  81.                         // 如果没有播放声音...
  82.                         if(!audio.isPlaying)
  83.                                 // ... 播放逃生的音乐文件.
  84.                                 audio.Play();
  85.                         
  86.                         // 如果时间超过了场景结束时间..
  87.                         if(timer >= timeToEndLevel)
  88.                                 // ... 场景结束.
  89.                                 sceneFadeInOut.EndScene();
  90.                 }
  91.         }


  92. }
复制代码
JS
  1. #pragma strict

  2. public var timeToDoorsClose : float = 2f;           // Time since the player entered the lift before the doors close.
  3. public var timeToLiftStart : float = 3f;            // Time since the player entered the lift before it starts to move.
  4. public var timeToEndLevel : float = 6f;             // Time since the player entered the lift before the level ends.
  5. public var liftSpeed : float = 3f;                  // The speed at which the lift moves.


  6. private var player : GameObject;                    // Reference to the player.
  7. private var playerAnim : Animator;                  // Reference to the players animator component.
  8. private var hash : HashIDs;                         // Reference to the HashIDs script.
  9. private var camMovement : CameraMovement;           // Reference to the camera movement script.
  10. private var sceneFadeInOut : SceneFadeInOut;        // Reference to the SceneFadeInOut script.
  11. private var liftDoorsTracking : LiftDoorsTracking;  // Reference to LiftDoorsTracking script.
  12. private var playerInLift : boolean;                 // Whether the player is in the lift or not.
  13. private var timer : float;                          // Timer to determine when the lift moves and when the level ends.


  14. function Awake ()
  15. {
  16.     // Setting up references.
  17.     player = GameObject.FindGameObjectWithTag(Tags.player);
  18.     playerAnim = player.GetComponent(Animator);
  19.     hash = GameObject.FindGameObjectWithTag(Tags.gameController).GetComponent(HashIDs);
  20.     camMovement = Camera.main.gameObject.GetComponent(CameraMovement);
  21.     sceneFadeInOut = GameObject.FindGameObjectWithTag(Tags.fader).GetComponent(SceneFadeInOut);
  22.     liftDoorsTracking = GetComponent(LiftDoorsTracking);
  23. }


  24. function OnTriggerEnter (other : Collider)
  25. {
  26.     // If the colliding gameobject is the player...
  27.     if(other.gameObject == player)
  28.         // ... the player is in the lift.
  29.         playerInLift = true;
  30. }


  31. function OnTriggerExit (other : Collider)
  32. {
  33.     // If the player leaves the trigger area...
  34.     if(other.gameObject == player)
  35.     {
  36.         // ... reset the timer, the player is no longer in the lift and unparent the player from the lift.
  37.         playerInLift = false;
  38.         timer = 0;
  39.     }
  40. }


  41. function Update ()
  42. {
  43.     // If the player is in the lift...
  44.     if(playerInLift)
  45.         // ... activate the lift.
  46.         LiftActivation();
  47.    
  48.     // If the timer is less than the time before the doors close...
  49.     if(timer < timeToDoorsClose)
  50.         // ... the inner doors should follow the outer doors.
  51.         liftDoorsTracking.DoorFollowing();
  52.     else
  53.         // Otherwise the doors should close.
  54.         liftDoorsTracking.CloseDoors();
  55. }


  56. function LiftActivation ()
  57. {
  58.     // Increment the timer by the amount of time since the last frame.
  59.     timer += Time.deltaTime;
  60.    
  61.     // If the timer is greater than the amount of time before the lift should start...
  62.     if(timer >= timeToLiftStart)
  63.     {
  64.         // ... stop the player and the camera moving and parent the player to the lift.
  65.         playerAnim.SetFloat(hash.speedFloat,0f);
  66.         camMovement.enabled = false;
  67.         player.transform.parent = transform;
  68.         
  69.         // Move the lift upwards.
  70.         transform.Translate(Vector3.up * liftSpeed * Time.deltaTime);
  71.         
  72.         // If the audio clip isn't playing...
  73.         if(!audio.isPlaying)
  74.             // ... play the clip.
  75.             audio.Play();
  76.         
  77.         // If the timer is greater than the amount of time before the level should end...
  78.         if(timer >= timeToEndLevel)
  79.             // ... call the EndScene function.
  80.             sceneFadeInOut.EndScene();
  81.     }
  82. }
复制代码

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2024-5-23 20:32

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表