简体   繁体   中英

Door problems in Unity3d C#

Hi so i'm using C# in Unity3d and am having a problem with my door code. Basically i have a detection cube that is the child of the actual door, this code is attached to the detection cube. My problem is that the door is flipping horizontal and being moved up and it's just freaking out. My mistake is probably obvious but i'm new to unity so please bear with me. Any help is appreciated, thank you in advance.(also don't know if this is helpful but due to modelling problems (AKA i don't know why) the door is normally face down horizontal like it's been knocked down so it's rotated 90 degrees so it's upright)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Door : MonoBehaviour
{
    public float doorOpenAngle = 90.0f;
    public float doorCloseAngle = 0.0f;
    public float doorAnimSpeed = 2.0f;
    private Quaternion doorOpen = Quaternion.identity;
    private Quaternion doorClose = Quaternion.identity;
    public bool doorStatus = false;
    private bool doorMoving = false;
    public GameObject door;


    void Start()
    {
        doorStatus = false;
        doorOpen = Quaternion.Euler(-90, doorOpenAngle, 0);
        doorClose = Quaternion.Euler(-90, doorCloseAngle, 0);
    }

    void Update()
    {
    }

    public IEnumerator moveDoor(Quaternion destination)
    {
        doorMoving = true;
        while (Quaternion.Angle(transform.localRotation, destination) > 4.0f)
        {
            door.transform.localRotation = Quaternion.Slerp(transform.localRotation, destination, Time.deltaTime * doorAnimSpeed);
            yield return null;
        }
        doorStatus = !doorStatus;
        doorMoving = false;
        yield return null;
    }


    private void OnTriggerStay(Collider other)
    {
        other = GameObject.Find("FPSController").GetComponent<Collider>();
        if (Input.GetKeyDown(KeyCode.E))
        {
            if (doorMoving == false)
            {
                if (doorStatus)
                    StartCoroutine(this.moveDoor(doorOpen));
                else
                    StartCoroutine(this.moveDoor(doorClose));
            }
        }
    }

}

The end goal here is to just get the door to rotate 90 degrees.

Add this to the end of your Start function so you know can verify your angles are what you think they are:

door.transform.localRotation = doorClose;

setting the start angle outside of the slerp should fix part of the problem. I also changed the conditions on the while loop to be time based.

float elapsed = 0f;
Quaternion startRotation = door.transform.localRotation;
while (elapsed <= 1f/doorAnimSpeed){
    elapsed += Time.deltaTime;
    door.transform.localRotation = Quaternion.Slerp(startRotation, destination, elapsed * doorAnimSpeed);
    yield return null;
}

You might also want to add an empty game object as a child, and add your door model to that. You can then rotate that empty however you like, and only rotate the parent on one axis when you need to open and close it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM