예제 #1
0
        private void Awake()
        {
            Transform[] childTransforms = gameObject.GetComponentsInChildren <Transform>();
            drawerPulls = new List <Transform>();

            foreach (Transform t in childTransforms)
            {
                if (t.name == "SlidingPart")
                {
                    slidingPart = t;
                }
                else if (t.name.Contains("DrawerPull") && t.gameObject.GetComponent <FPEInteractableActivateScript>())
                {
                    drawerPulls.Add(t);
                }
                else if (t.name.Contains("OpenHitHelper") && t.gameObject.GetComponent <FPEDoorAndDrawerHitHelper>())
                {
                    openHitHelper = t.gameObject.GetComponent <FPEDoorAndDrawerHitHelper>();
                }
                else if (t.name.Contains("CloseHitHelper") && t.gameObject.GetComponent <FPEDoorAndDrawerHitHelper>())
                {
                    closeHitHelper = t.gameObject.GetComponent <FPEDoorAndDrawerHitHelper>();
                }
                else if (t.name == "SafeZone")
                {
                    safeZone = t;
                }
            }

            if (!slidingPart || drawerPulls.Count == 0)
            {
                Debug.Log("FPEDrawer:: Drawer '" + gameObject.name + "' was missing key component(s). Attempting to fix.", gameObject);

                // Attempt some automatic fixes
#if UNITY_EDITOR
                if (slidingPart == null)
                {
                    GameObject slidingPartFix = new GameObject("SlidingPart");
                    slidingPartFix.transform.parent        = gameObject.transform;
                    slidingPartFix.transform.localPosition = Vector3.zero;
                    slidingPart = slidingPartFix.transform;
                }

                if (drawerPulls.Count == 0)
                {
                    GameObject drawerPullFix = new GameObject("DrawerPull");
                    drawerPullFix.AddComponent <BoxCollider>();
                    drawerPullFix.AddComponent <FPEInteractableActivateScript>();
                    drawerPullFix.transform.parent        = slidingPart;
                    drawerPullFix.transform.localPosition = Vector3.zero;
                    drawerPulls.Add(drawerPullFix.transform);
                    Debug.LogWarning("FPEDrawer:: Drawer '" + gameObject.name + "' DrawerPull has no Activation Event. Drawer Pull interaction will do nothing until assigned. Also remember to make the Event Fire Type be 'EVERYTIME'.", gameObject);
                }
#endif
            }

            drawerSlideDistancePerSecond = Mathf.Abs(slideDistance / slideOpenTimeInSeconds);
            closedPosition   = slidingPart.localPosition;
            closedPosition.z = 0f;
            openedPosition   = closedPosition + new Vector3(0f, 0f, slideDistance);

            if (startOpened)
            {
                currentDrawerState = eDrawerState.OPEN;
                setDrawerPullInteractionStrings(closeDrawerString);
            }
            else
            {
                setDrawerPullInteractionStrings(openDrawerString);
            }

            if (startExternallyLocked)
            {
                externallyLocked = true;
            }

            if (startInternallyLocked)
            {
                internallyLocked = true;
            }

            drawerSpeaker = gameObject.GetComponent <AudioSource>();

            // Error Checking //
            if (stopIfDrawerHitsSomething && (openHitHelper == null || closeHitHelper == null))
            {
                Debug.LogError("FPEDrawer:: Drawer '" + gameObject.name + "' is configured to stop if drawer hits something, but it is missing child 'OpenHitHelper' or 'CloseHitHelper' FPEDoorAndDrawerHitHelper objects. Drawer will not stop when it hits something.", gameObject);
            }

            // Safe zone is optional, but is required if you've set the flag to auto move the player
            if (autoMovePlayerToSafeZone && !safeZone)
            {
                Debug.Log("FPEDrawer:: Drawer '" + gameObject.name + "' is configured to automatically move the player into safe zone, but has no safe zone defined. Attempting to fix");

                // Attempt some automatic fixes (assumes slide distance is 0.45, which is the default, which is likely
                // the value since these fixes really only happen if you just add this script to an empty game object)
#if UNITY_EDITOR
                GameObject safeZoneFix = new GameObject("SafeZone");
                safeZoneFix.transform.parent        = gameObject.transform;
                safeZoneFix.transform.localPosition = new Vector3(0.0f, 0.0f, 1.3f);
                safeZone = safeZoneFix.transform;
#endif
            }

            if (playSounds && drawerSpeaker == null)
            {
                Debug.Log("FPEDrawer:: Drawer '" + gameObject.name + "' was configured to play sounds but had no AudioSource. Adding AudioSource.", gameObject);

                // Attempt some automatic fixes
#if UNITY_EDITOR
                gameObject.AddComponent <AudioSource>();
                drawerSpeaker = gameObject.GetComponent <AudioSource>();
#endif
            }
        }
        protected override void Awake()
        {
            base.Awake();

            Transform[] childTransforms = gameObject.GetComponentsInChildren <Transform>();
            doorHandles  = new List <Transform>();
            myHitHelpers = new FPEDoorAndDrawerHitHelper[2];

            foreach (Transform t in childTransforms)
            {
                if (t.name == "SwingingPart")
                {
                    swingingPart = t;
                }
                else if (t.name.Contains("DoorHandle") && t.gameObject.GetComponent <FPEInteractableActivateScript>())
                {
                    doorHandles.Add(t);
                }
                else if (t.name == "SwingInSide")
                {
                    swingInSide = t;
                }
                else if (t.name == "SwingOutSide")
                {
                    swingOutSide = t;
                }
                else if (t.name == "SafeZoneSwingIn")
                {
                    safeZoneSwingIn = t;
                }
                else if (t.name == "SafeZoneSwingOut")
                {
                    safeZoneSwingOut = t;
                }
                else if (t.name == "DoorActionLookTarget")
                {
                    doorActionLookTarget = t;
                }
                else if (t.name == "HitHelperOpen" && t.gameObject.GetComponent <FPEDoorAndDrawerHitHelper>())
                {
                    myHitHelpers[(int)eDoorActionType.OPEN] = t.gameObject.GetComponent <FPEDoorAndDrawerHitHelper>();
                }
                else if (t.name == "HitHelperClose" && t.gameObject.GetComponent <FPEDoorAndDrawerHitHelper>())
                {
                    myHitHelpers[(int)eDoorActionType.CLOSE] = t.gameObject.GetComponent <FPEDoorAndDrawerHitHelper>();
                }
            }

            maxSwingDegreesPerSecond = Mathf.Abs(swingAngle / swingOpenTimeInSeconds);
            closedRotation           = Quaternion.identity;
            openRotation             = closedRotation * Quaternion.Euler(new Vector3(0f, swingAngle, 0f));

            if (startOpened)
            {
                currentDoorState = eDoorState.OPEN;
                setDoorHandleInteractionStrings(closeDoorString);
            }
            else
            {
                setDoorHandleInteractionStrings(openDoorString);
            }

            doorSpeaker = gameObject.GetComponent <AudioSource>();

            // Error Checking //

            // Doors must have swinging parts and at least one door handle
            if (!swingingPart || doorHandles.Count == 0)
            {
                Debug.Log("FPESwingingDoor:: Door '" + gameObject.name + "' is missing a required part. Attempting to fix.", gameObject);

                // Attempt some automatic fixes
#if UNITY_EDITOR
                if (swingingPart == null)
                {
                    GameObject swingingPartFix = new GameObject("SwingingPart");
                    swingingPartFix.transform.parent        = gameObject.transform;
                    swingingPartFix.transform.localPosition = new Vector3(-1.0f, 1.5f, -0.251f);
                    swingingPart = swingingPartFix.transform;
                }

                if (doorHandles.Count == 0)
                {
                    GameObject doorHandleFix = new GameObject("DoorHandle");
                    doorHandleFix.AddComponent <BoxCollider>();
                    doorHandleFix.AddComponent <FPEInteractableActivateScript>();
                    doorHandleFix.transform.parent        = swingingPart;
                    doorHandleFix.transform.localPosition = Vector3.zero;
                    doorHandles.Add(doorHandleFix.transform);
                    Debug.LogWarning("FPEDoor:: Door '" + gameObject.name + "' DoorHandle child has no Activation Event assigned. Door Handle interaction will do nothing until it is assigned. Also remember to make the Event Fire Type be 'EVERYTIME'.", gameObject);
                }
#endif
            }

            // Some angles are not valid and will result in weird first time swings
            if (swingAngle == 0f)
            {
                Debug.LogWarning("FPESwingingDoor:: Door '" + gameObject.name + "' has a swingAngle of zero. This door will not work.", gameObject);
            }
            else
            {
                float deltaA = Quaternion.Angle(swingingPart.localRotation, openRotation);
                float deltaB = Quaternion.Angle(swingingPart.localRotation, closedRotation);

                if (Mathf.Abs(deltaA) > Mathf.Abs(swingAngle) || Mathf.Abs(deltaB) > Mathf.Abs(swingAngle))
                {
                    Debug.LogWarning("FPESwingingDoor:: Door '" + gameObject.name + "''s 'SwingingPart' starting rotation is outside defined swingAngle's range. First open/close actions will not behave as expected.", gameObject);
                }
            }

            if (stopIfDoorHitsSomething && (myHitHelpers[(int)eDoorActionType.OPEN] == null || myHitHelpers[(int)eDoorActionType.CLOSE] == null))
            {
                Debug.LogWarning("FPESwingingDoor:: Door '" + gameObject.name + "' is configured to stop if door hits something, but it is missing child FPEDoorAndDrawerHitHelper objects 'HitHelperOpen' and/or 'HitHelperClose'. Door will not stop when it hits something.", gameObject);
            }

            // Safe zones are optional, but are required if you've set the flag to auto move the player
            if (autoMovePlayerToSafeZones && (!swingInSide || !swingOutSide || !safeZoneSwingIn || !safeZoneSwingOut || !doorActionLookTarget))
            {
                Debug.Log("FPESwingingDoor:: Door '" + gameObject.name + "' is configured to automatically move the player into safe zones, but has no safe zones defined. Attempting to fix");

                // Attempt some automatic fixes (these assume swingAngle is 135, which is the default, which is likely
                // the value since these fixes really only happen if you just add this script to an empty game object)
#if UNITY_EDITOR
                GameObject interactionZoneFix = new GameObject("PlayerInteractionZones");
                interactionZoneFix.transform.parent        = gameObject.transform;
                interactionZoneFix.transform.localPosition = Vector3.zero;

                if (swingInSide == null)
                {
                    GameObject swingInsideFix = new GameObject("SwingInSide");
                    swingInsideFix.transform.parent        = interactionZoneFix.transform;
                    swingInsideFix.transform.localPosition = new Vector3(0.345f, 0.474f, 0.054f);
                    swingInSide = swingInsideFix.transform;
                }

                if (swingOutSide == null)
                {
                    GameObject swingOutSideFix = new GameObject("SwingOutSide");
                    swingOutSideFix.transform.parent        = interactionZoneFix.transform;
                    swingOutSideFix.transform.localPosition = new Vector3(0.345f, 0.474f, -1.219f);
                    swingOutSide = swingOutSideFix.transform;
                }

                if (safeZoneSwingIn == null)
                {
                    GameObject safeZoneSwingInFix = new GameObject("SafeZoneSwingIn");
                    safeZoneSwingInFix.transform.parent        = interactionZoneFix.transform;
                    safeZoneSwingInFix.transform.localPosition = new Vector3(-0.07f, 0.98f, 0.92f);
                    safeZoneSwingIn = safeZoneSwingInFix.transform;
                }

                if (safeZoneSwingOut == null)
                {
                    GameObject safeZoneSwingOutFix = new GameObject("SafeZoneSwingOut");
                    safeZoneSwingOutFix.transform.parent        = interactionZoneFix.transform;
                    safeZoneSwingOutFix.transform.localPosition = new Vector3(1.0f, 0.98f, -1.66f);
                    safeZoneSwingOut = safeZoneSwingOutFix.transform;
                }

                if (doorActionLookTarget == null)
                {
                    GameObject lookTargetFix = new GameObject("DoorActionLookTarget");
                    lookTargetFix.transform.parent        = interactionZoneFix.transform;
                    lookTargetFix.transform.localPosition = new Vector3(0.741f, 1.568f, -0.161f);
                    doorActionLookTarget = lookTargetFix.transform;
                }
#endif
            }

            if (playSounds && doorSpeaker == null)
            {
                Debug.Log("FPESwingingDoor:: Door '" + gameObject.name + "' is configured to play sounds but has no AudioSource. Attempting to fix.", gameObject);

                // Attempt some automatic fixes
#if UNITY_EDITOR
                gameObject.AddComponent <AudioSource>();
                doorSpeaker = gameObject.GetComponent <AudioSource>();
#endif
            }
        }
예제 #3
0
        protected override void Awake()
        {
            base.Awake();

            Transform[] childTransforms = gameObject.GetComponentsInChildren <Transform>();
            doorHandles  = new List <Transform>();
            myHitHelpers = new FPEDoorAndDrawerHitHelper[2];

            foreach (Transform t in childTransforms)
            {
                if (t.name == "SwingingPart")
                {
                    swingingPart = t;
                }
                else if (t.name.Contains("DoorHandle") && t.gameObject.GetComponent <FPEInteractableActivateScript>())
                {
                    doorHandles.Add(t);
                }
                else if (t.name == "LeftHandSide")
                {
                    leftHandSide = t;
                }
                else if (t.name == "RightHandSide")
                {
                    rightHandSide = t;
                }
                else if (t.name == "HitHelperLeft" && t.gameObject.GetComponent <FPEDoorAndDrawerHitHelper>())
                {
                    myHitHelpers[(int)eDoorSide.LEFT] = t.gameObject.GetComponent <FPEDoorAndDrawerHitHelper>();
                }
                else if (t.name == "HitHelperRight" && t.gameObject.GetComponent <FPEDoorAndDrawerHitHelper>())
                {
                    myHitHelpers[(int)eDoorSide.RIGHT] = t.gameObject.GetComponent <FPEDoorAndDrawerHitHelper>();
                }
            }

            maxSwingDegreesPerSecond = Mathf.Abs(swingAngle / swingOpenTimeInSeconds);
            closedRotation           = Quaternion.identity;
            openRotationLeft         = closedRotation * Quaternion.Euler(new Vector3(0f, swingAngle, 0f));
            openRotationRight        = closedRotation * Quaternion.Euler(new Vector3(0f, -swingAngle, 0f));
            currentSwingOpenRotation = openRotationLeft;

            if (startOpened)
            {
                currentDoorState = eDoorState.OPEN;
                setDoorHandleInteractionStrings(closeDoorString);
            }
            else
            {
                setDoorHandleInteractionStrings(openDoorString);
            }

            doorSpeaker = gameObject.GetComponent <AudioSource>();


            // Error Checking //

            if (stopIfDoorHitsSomething && (myHitHelpers[(int)eDoorSide.LEFT] == null || myHitHelpers[(int)eDoorSide.RIGHT] == null))
            {
                Debug.LogWarning("FPEAlwaysSwingOutDoor:: Door '" + gameObject.name + "' is configured to stop if door hits something, but it is missing its no child FPEDoorAndDrawerHitHelper objects 'HitHelperLeft' and/or 'HitHelperRight'. Door will not stop when it hits something.", gameObject);
            }

            // Doors must have swinging parts and at least one door handle
            if (!swingingPart || doorHandles.Count == 0 || !leftHandSide || !rightHandSide)
            {
                Debug.Log("FPEAlwaysSwingOutDoor:: Door '" + gameObject.name + "' is missing a required parts. Attempting to fix.", gameObject);

                // Attempt some automatic fixes
#if UNITY_EDITOR
                if (swingingPart == null)
                {
                    GameObject swingingPartFix = new GameObject("SwingingPart");
                    swingingPartFix.transform.parent        = gameObject.transform;
                    swingingPartFix.transform.localPosition = Vector3.zero;
                    swingingPart = swingingPartFix.transform;
                }

                if (doorHandles.Count == 0)
                {
                    GameObject doorHandleFix = new GameObject("DoorHandle");
                    doorHandleFix.AddComponent <BoxCollider>();
                    doorHandleFix.AddComponent <FPEInteractableActivateScript>();
                    doorHandleFix.transform.parent        = swingingPart;
                    doorHandleFix.transform.localPosition = Vector3.zero;
                    doorHandles.Add(doorHandleFix.transform);
                    Debug.LogWarning("FPEDoor:: Door '" + gameObject.name + "' DoorHandle child has no Activation Event assigned. Door Handle interaction will do nothing until it is assigned. Also remember to make the Event Fire Type be 'EVERYTIME'.", gameObject);
                }

                if (leftHandSide == null || rightHandSide == null)
                {
                    GameObject interactionZoneFix = new GameObject("PlayerInteractionZones");
                    interactionZoneFix.transform.parent        = gameObject.transform;
                    interactionZoneFix.transform.localPosition = Vector3.zero;

                    if (leftHandSide == null)
                    {
                        GameObject lhsFix = new GameObject("LeftHandSide");
                        lhsFix.transform.parent        = interactionZoneFix.transform;
                        lhsFix.transform.localPosition = Vector3.zero - (transform.forward * 1f);
                        leftHandSide = lhsFix.transform;
                    }

                    if (rightHandSide == null)
                    {
                        GameObject rhsFix = new GameObject("RightHandSide");
                        rhsFix.transform.parent        = interactionZoneFix.transform;
                        rhsFix.transform.localPosition = Vector3.zero + (transform.forward * 1f);
                        rightHandSide = rhsFix.transform;
                    }
                }
#endif
            }



            // Some angles are not valid and will result in weird first time swings
            if (swingAngle == 0f)
            {
                Debug.LogWarning("FPEAlwaysSwingOutDoor:: Door '" + gameObject.name + "' has a swingAngle of zero. This door will not work.", gameObject);
            }
            else
            {
                float deltaA1 = Quaternion.Angle(swingingPart.localRotation, openRotationLeft);
                float deltaA2 = Quaternion.Angle(swingingPart.localRotation, openRotationRight);
                float deltaB  = Quaternion.Angle(swingingPart.localRotation, closedRotation);

                if ((Mathf.Abs(deltaA1) > Mathf.Abs(swingAngle) && Mathf.Abs(deltaA2) > Mathf.Abs(swingAngle)) || Mathf.Abs(deltaB) > Mathf.Abs(swingAngle))
                {
                    Debug.LogWarning("FPEAlwaysSwingOutDoor:: Door '" + gameObject.name + "''s 'SwingingPart' starting rotation is outside defined swingAngle's range. First open/close actions will not behave as expected.", gameObject);
                }
            }

            if (playSounds && doorSpeaker == null)
            {
                Debug.Log("FPEAlwaysSwingOutDoor:: Door '" + gameObject.name + "' is configured to play sounds but has no AudioSource. Attempting to fix.", gameObject);

                // Attempt some automatic fixes
#if UNITY_EDITOR
                gameObject.AddComponent <AudioSource>();
                doorSpeaker = gameObject.GetComponent <AudioSource>();
#endif
            }
        }