Unity 3D门脚本角度转换

时间:2015-04-23 14:21:01

标签: c# unity3d

我为开门和关门编写以下代码:

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;
                    }
                }
            }
        }
    }
}

问题是门没有以正确的角度打开,如果我关上门,则不会回到起始位置。 也许somebosy有个主意?

1 个答案:

答案 0 :(得分:0)

尝试将角度更改为-90,它总是发生在我身上。

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

第二个:

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