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

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

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

查看: 1811|回复: 0

6-4 Double Doors 对开门的逻辑设定

[复制链接]
发表于 2013-10-2 08:28:23 | 显示全部楼层 |阅读模式
对开门的逻辑设定
LiftDoorsTracking
C#
  1. using UnityEngine;
  2. using System.Collections;

  3. public class LiftDoorsTracking : MonoBehaviour
  4. {
  5.     public float doorSpeed = 7f;            // 门关闭的速度.
  6.    
  7.    
  8.     private Transform leftOuterDoor;        // 红色外门左扇门.
  9.     private Transform rightOuterDoor;       // 红色外门右扇门.
  10.     private Transform leftInnerDoor;        // 电梯内栅栏门左侧门.
  11.     private Transform rightInnerDoor;       // 电梯内栅栏门右侧门.
  12.     private float leftClosedPosX;           // 记录左扇门关闭时的坐标.
  13.     private float rightClosedPosX;          // 记录右扇门关闭时的坐标.
  14.    
  15.    
  16.     void Awake ()
  17.     {
  18.         // 初始化变量.
  19.         leftOuterDoor = GameObject.Find("door_exitOuter_left_001").transform;
  20.         rightOuterDoor = GameObject.Find("door_exitOuter_right_001").transform;
  21.         leftInnerDoor = GameObject.Find("door_exitInner_left_001").transform;
  22.         rightInnerDoor = GameObject.Find("door_exitInner_right_001").transform;
  23.         
  24.         // 设置门关闭时的坐标位置.
  25.         leftClosedPosX = leftInnerDoor.position.x;
  26.         rightClosedPosX = rightInnerDoor.position.x;
  27.     }
  28.    
  29.    
  30.     void MoveDoors (float newLeftXTarget, float newRightXTarget)
  31.     {
  32.         // 创建一个布尔值记录从leftInnerDoor.position.x到newLeftXTarget之间的插值.
  33.         float newX = Mathf.Lerp(leftInnerDoor.position.x, newLeftXTarget, doorSpeed * Time.deltaTime);
  34.         
  35.         // 根据新的插值移动左扇门的位置.
  36.         leftInnerDoor.position = new Vector3(newX, leftInnerDoor.position.y, leftInnerDoor.position.z);
  37.         
  38.         // 通过布尔值记录从rightInnerDoor.position.x到newRightXTarget之间的插值.
  39.         newX = Mathf.Lerp(rightInnerDoor.position.x, newRightXTarget, doorSpeed * Time.deltaTime);
  40.         
  41.         // 根据新的插值移动右扇门的位置.
  42.         rightInnerDoor.position = new Vector3(newX, rightInnerDoor.position.y, rightInnerDoor.position.z);
  43.     }
  44.    
  45.    
  46.     public void DoorFollowing ()
  47.     {
  48.         // 移动门到 外面两扇门的位置.
  49.         MoveDoors(leftOuterDoor.position.x, rightOuterDoor.position.x);
  50.     }
  51.    
  52.    
  53.     public void CloseDoors ()
  54.     {
  55.         // 移动门到 栅栏门的初始位置.
  56.         MoveDoors(leftClosedPosX, rightClosedPosX);
  57.     }
  58. }
复制代码
JS
  1. #pragma strict

  2. public var doorSpeed : float = 7f;          // How quickly the inner doors will track the outer doors.


  3. private var leftOuterDoor : Transform;      // Reference to the transform of the left outer door.
  4. private var rightOuterDoor : Transform;     // Reference to the transform of the right outer door.
  5. private var leftInnerDoor : Transform;      // Reference to the transform of the left inner door.
  6. private var rightInnerDoor : Transform;     // Reference to the transform of the right inner door.
  7. private var leftClosedPosX : float;         // The initial x component of position of the left doors.
  8. private var rightClosedPosX : float;        // The initial x component of position of the right doors.


  9. function Awake ()
  10. {
  11.     // Setting up the references.
  12.     leftOuterDoor = GameObject.Find("door_exitOuter_left_001").transform;
  13.     rightOuterDoor = GameObject.Find("door_exitOuter_right_001").transform;
  14.     leftInnerDoor = GameObject.Find("door_exitInner_left_001").transform;
  15.     rightInnerDoor = GameObject.Find("door_exitInner_right_001").transform;
  16.    
  17.     // Setting the closed x position of the doors.
  18.     leftClosedPosX = leftInnerDoor.position.x;
  19.     rightClosedPosX = rightInnerDoor.position.x;
  20. }


  21. function MoveDoors (newLeftXTarget : float, newRightXTarget : float)
  22. {
  23.     // Create a float that is a proportion of the distance from the left inner door's x position to it's target x position.
  24.     var newX : float = Mathf.Lerp(leftInnerDoor.position.x, newLeftXTarget, doorSpeed * Time.deltaTime);
  25.    
  26.     // Move the left inner door to it's new position proportionally closer to it's target.
  27.     leftInnerDoor.position = new Vector3(newX, leftInnerDoor.position.y, leftInnerDoor.position.z);
  28.    
  29.     // Reassign the float for the right door's x position.
  30.     newX = Mathf.Lerp(rightInnerDoor.position.x, newRightXTarget, doorSpeed * Time.deltaTime);
  31.    
  32.     // Move the right inner door similarly.
  33.     rightInnerDoor.position = new Vector3(newX, rightInnerDoor.position.y, rightInnerDoor.position.z);
  34. }


  35. public function DoorFollowing ()
  36. {
  37.     // Move the inner doors towards the outer doors.
  38.     MoveDoors(leftOuterDoor.position.x, rightOuterDoor.position.x);
  39. }


  40. public function CloseDoors ()
  41. {
  42.     // Move the inner doors towards their closed position.
  43.     MoveDoors(leftClosedPosX, rightClosedPosX);
  44. }
复制代码

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

GMT+8, 2024-11-16 04:04

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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