Unity3D VR推拉门

许多VR项目中都会有推拉门这样的需求,最近遇到了需求是在推拉门关闭状态时,当人拿住把手时,需要先让推拉门产生一个外推向走廊的动画,然后再变成可以推拉的门。由于项目原因不方便放上视频,有同样需求的可以参考代码。

using UnityEngine;
using System.Collections;
using DG.Tweening;

public class XXXDoor : MonoBehaviour {
    /// 
    /// 是否门可以使用
    /// 
    public bool canUse = false;

    public enum DoorState
    {
        /// 
        /// 门处于开着
        /// 
        DoorOpen,
        /// 
        /// 门处于关闭
        /// 
        DoorClose
    }
    public DoorState doorState;

    /// 
    /// 门是否正在播放动画
    /// 
    public bool isAniming = false;
    /// 
    /// 关门动画触发位置
    /// 
    public Vector3 correctPos;
    /// 
    /// 门完全关闭位置
    /// 
    public Vector3 closePos;

    private void Start()
    {
    }

    private void Update()
    {
        if (canUse)
        if (doorState == DoorState.DoorOpen&&!isAniming&&transform.localPosition.z
    /// 关闭到打开的动画,由玩家操作调用
    /// 
    public void CloseToOpenAnim()
    {
        if (canUse)
        {
            isAniming = true;
            transform.DOLocalMoveX(correctPos.x, .5f);
            transform.DOLocalMoveZ(correctPos.z + .1f, 1.0f);
            StartCoroutine(CloseAnimState(0));
        }
    }
    /// 
    /// 打开到关闭的动画,由门自己判定
    /// 
    void OpenToCloseAnim()
    {
        isAniming = true;
        transform.DOLocalMoveZ(closePos.z, .5f);
        transform.DOLocalMoveX(closePos.x, 1.0f);
        StartCoroutine(CloseAnimState(1));
    }
    IEnumerator CloseAnimState(int i)
    {
        yield return new WaitForSeconds(1.1f);
        switch (i)
        {
            case 0:
                doorState = DoorState.DoorOpen;
                break;
            case 1:
                doorState = DoorState.DoorClose;
                break;
            default:
                break;
        }
        isAniming = false;
    }
}


using UnityEngine;
using System.Collections;

public class XXXDoorHandle : UseableObject {

    RoomDoor roomDoor;

    protected override void Start()
    {
        roomDoor = GetComponentInParent();
        base.Start();
    }
    /// 
    /// 由手柄扳机键按住时, 持续调用
    /// 
    /// 
    public override void HoldIt(GameObject controller)
    {
        if(!roomDoor.isAniming)
            switch (roomDoor.doorState)
            {
                case XXXDoor.DoorState.DoorOpen:
                    base.HoldIt(controller);
                    break;
                case XXXDoor.DoorState.DoorClose:
                    roomDoor.CloseToOpenAnim();
                    break;
                default:
                    break;
            }
    }
}



你可能感兴趣的:(Unity,游戏,unity,对象,应用)