|
对开门的逻辑设定
LiftDoorsTracking
C#- using UnityEngine;
- using System.Collections;
- public class LiftDoorsTracking : MonoBehaviour
- {
- public float doorSpeed = 7f; // 门关闭的速度.
-
-
- private Transform leftOuterDoor; // 红色外门左扇门.
- private Transform rightOuterDoor; // 红色外门右扇门.
- private Transform leftInnerDoor; // 电梯内栅栏门左侧门.
- private Transform rightInnerDoor; // 电梯内栅栏门右侧门.
- private float leftClosedPosX; // 记录左扇门关闭时的坐标.
- private float rightClosedPosX; // 记录右扇门关闭时的坐标.
-
-
- void Awake ()
- {
- // 初始化变量.
- leftOuterDoor = GameObject.Find("door_exitOuter_left_001").transform;
- rightOuterDoor = GameObject.Find("door_exitOuter_right_001").transform;
- leftInnerDoor = GameObject.Find("door_exitInner_left_001").transform;
- rightInnerDoor = GameObject.Find("door_exitInner_right_001").transform;
-
- // 设置门关闭时的坐标位置.
- leftClosedPosX = leftInnerDoor.position.x;
- rightClosedPosX = rightInnerDoor.position.x;
- }
-
-
- void MoveDoors (float newLeftXTarget, float newRightXTarget)
- {
- // 创建一个布尔值记录从leftInnerDoor.position.x到newLeftXTarget之间的插值.
- float newX = Mathf.Lerp(leftInnerDoor.position.x, newLeftXTarget, doorSpeed * Time.deltaTime);
-
- // 根据新的插值移动左扇门的位置.
- leftInnerDoor.position = new Vector3(newX, leftInnerDoor.position.y, leftInnerDoor.position.z);
-
- // 通过布尔值记录从rightInnerDoor.position.x到newRightXTarget之间的插值.
- newX = Mathf.Lerp(rightInnerDoor.position.x, newRightXTarget, doorSpeed * Time.deltaTime);
-
- // 根据新的插值移动右扇门的位置.
- rightInnerDoor.position = new Vector3(newX, rightInnerDoor.position.y, rightInnerDoor.position.z);
- }
-
-
- public void DoorFollowing ()
- {
- // 移动门到 外面两扇门的位置.
- MoveDoors(leftOuterDoor.position.x, rightOuterDoor.position.x);
- }
-
-
- public void CloseDoors ()
- {
- // 移动门到 栅栏门的初始位置.
- MoveDoors(leftClosedPosX, rightClosedPosX);
- }
- }
复制代码 JS- #pragma strict
- public var doorSpeed : float = 7f; // How quickly the inner doors will track the outer doors.
- private var leftOuterDoor : Transform; // Reference to the transform of the left outer door.
- private var rightOuterDoor : Transform; // Reference to the transform of the right outer door.
- private var leftInnerDoor : Transform; // Reference to the transform of the left inner door.
- private var rightInnerDoor : Transform; // Reference to the transform of the right inner door.
- private var leftClosedPosX : float; // The initial x component of position of the left doors.
- private var rightClosedPosX : float; // The initial x component of position of the right doors.
- function Awake ()
- {
- // Setting up the references.
- leftOuterDoor = GameObject.Find("door_exitOuter_left_001").transform;
- rightOuterDoor = GameObject.Find("door_exitOuter_right_001").transform;
- leftInnerDoor = GameObject.Find("door_exitInner_left_001").transform;
- rightInnerDoor = GameObject.Find("door_exitInner_right_001").transform;
-
- // Setting the closed x position of the doors.
- leftClosedPosX = leftInnerDoor.position.x;
- rightClosedPosX = rightInnerDoor.position.x;
- }
- function MoveDoors (newLeftXTarget : float, newRightXTarget : float)
- {
- // Create a float that is a proportion of the distance from the left inner door's x position to it's target x position.
- var newX : float = Mathf.Lerp(leftInnerDoor.position.x, newLeftXTarget, doorSpeed * Time.deltaTime);
-
- // Move the left inner door to it's new position proportionally closer to it's target.
- leftInnerDoor.position = new Vector3(newX, leftInnerDoor.position.y, leftInnerDoor.position.z);
-
- // Reassign the float for the right door's x position.
- newX = Mathf.Lerp(rightInnerDoor.position.x, newRightXTarget, doorSpeed * Time.deltaTime);
-
- // Move the right inner door similarly.
- rightInnerDoor.position = new Vector3(newX, rightInnerDoor.position.y, rightInnerDoor.position.z);
- }
- public function DoorFollowing ()
- {
- // Move the inner doors towards the outer doors.
- MoveDoors(leftOuterDoor.position.x, rightOuterDoor.position.x);
- }
- public function CloseDoors ()
- {
- // Move the inner doors towards their closed position.
- MoveDoors(leftClosedPosX, rightClosedPosX);
- }
复制代码 |
|