简体   繁体   中英

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.

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

the second one:

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

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