简体   繁体   English

Unity 3D门脚本角度转换

[英]Unity 3D Door Script Angle Transformation

i write the following code for open and close doors: 我为打开和关闭门编写以下代码:

using UnityEngine;
using System.Collections;

public class LittleDoorScript : MonoBehaviour
{

    private bool doorOpen = false;
    private Ray ray;
    private RaycastHit hit;
    private float distance = 5.0f;
    public GameObject door;

    private void Update()
    {
        if (Input.GetKeyDown("e"))
        {
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, distance))
            {
                if(hit.collider.gameObject.name == door.name)
                {
                    if (!doorOpen)
                    {
                        Quaternion targetRotation = Quaternion.Euler(0.0f, 90.0f, 0.0f);
                        hit.transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation, 2.0f);
                        doorOpen = true;
                    }
                    else
                    {
                        Quaternion targetRotation2 = Quaternion.Euler(0.0f, 0.0f, 0.0f);
                        hit.transform.localRotation = Quaternion.Slerp(transform.localRotation, targetRotation2, 2.0f);
                        doorOpen = false;
                    }
                }
            }
        }
    }
}

the problem is the door not open in the right angle and if i close the door the not go back in the start position. 问题是门没有以正确的角度打开,如果我关上门,则门不能回到起始位置。 Maybe somebosy have an idea? 也许有人有个主意吗?

Try changing the angle to -90, it always happens to me. 尝试将角度更改为-90,这总是发生在我身上。

Quaternion targetRotation = Quaternion.Euler(0.0f, -90.0f, 0.0f);

the second one: 第二个:

Quaternion targetRotation2 = Quaternion.Euler(0.0f, 90.0f, 0.0f);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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