My script won't make negative rotations.

I have a script that functions as a door, and as is, it works perfectly. Here it is…

var smooth = 2.0;
var DoorOpenAngle = 90.0;
var opened : GameObject;
var closed : GameObject;
var key : GameObject;
var noKey : GameObject;
var button1 : UnityEngine.UI.Graphic;
var button2 : UnityEngine.UI.Graphic;
var button1Background : UnityEngine.UI.Graphic;
var button2Background : UnityEngine.UI.Graphic;
var locked1 : UnityEngine.UI.Graphic;
var locked2 : UnityEngine.UI.Graphic;
var fadeTime : float = 0;
var midValue : float = 0;
private var open : boolean;
private var enter : boolean;

private var defaultRot : Vector3;
private var openRot : Vector3;

function Start(){
    defaultRot = transform.eulerAngles;
    openRot = new Vector3 (defaultRot.x, defaultRot.y + DoorOpenAngle, defaultRot.z);
}

//Main function
function Update (){
    if(key.activeSelf)
    if(opened.activeSelf){
        //Open door
        transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, openRot, Time.deltaTime * smooth);
    }
   
    if(closed.activeSelf){
        //Close door
        transform.eulerAngles = Vector3.Slerp(transform.eulerAngles, defaultRot, Time.deltaTime * smooth);
    }

    if(Input.GetButtonDown("Interact") && enter)
        if(transform.eulerAngles.y > midValue)
        if (key.activeSelf){
        closed.SetActive (true);
        opened.SetActive (false);
    }
    if(Input.GetButtonDown("Interact") && enter)
        if(transform.eulerAngles.y < midValue)
            if (key.activeSelf){
        opened.SetActive (true);
        closed.SetActive (false);
}

    if(enter)
    {
        button1.color = Color.Lerp (button1.color, Color.green, fadeTime * Time.deltaTime);
        button2.color = Color.Lerp (button2.color, Color.green, fadeTime * Time.deltaTime);
        button1Background.color = Color.Lerp (button1Background.color, Color.black, fadeTime * Time.deltaTime);
        button2Background.color = Color.Lerp (button2Background.color, Color.black, fadeTime * Time.deltaTime);
    }

    if(enter == false)
    {
        button1.color = Color.Lerp (button1.color, Color.clear, fadeTime * Time.deltaTime);
        button2.color = Color.Lerp (button2.color, Color.clear, fadeTime * Time.deltaTime);
        button1Background.color = Color.Lerp (button1Background.color, Color.clear, fadeTime * Time.deltaTime);
        button2Background.color = Color.Lerp (button2Background.color, Color.clear, fadeTime * Time.deltaTime);
        locked1.color = Color.Lerp (locked1.color, Color.clear, fadeTime * Time.deltaTime);
        locked2.color = Color.Lerp (locked2.color, Color.clear, fadeTime * Time.deltaTime);
    }

    if(enter)
        if(noKey.activeSelf)
    {
            locked1.color = Color.Lerp (locked1.color, Color.green, fadeTime * Time.deltaTime);
            locked2.color = Color.Lerp (locked2.color, Color.green, fadeTime * Time.deltaTime);
        }

    if(enter)
        if(key.activeSelf)
        {
            locked1.color = Color.Lerp (locked1.color, Color.clear, fadeTime * Time.deltaTime);
            locked2.color = Color.Lerp (locked2.color, Color.clear, fadeTime * Time.deltaTime);
        }
}

//Activate the Main function when player is near the door
function OnTriggerEnter (other : Collider){
    if (other.gameObject.tag == "Player") {
        enter = true;
    }
}

    //Deactivate the Main function when player is go away from door
    function OnTriggerExit (other : Collider){
        if (other.gameObject.tag == "Player"){
            enter = false;
    }
}

Bit, I want it, instead of adding the DoorOpenAngle value, I want it to subtract it. The issue here is that when I change this, the door freaks out and starts spinning all over the place! How can I fix this?

Likely because you’re using Vector3 linear interpolation, so it takes the shortest Euclidean path to the target rotation. Try using Quaternions instead like this:

    if(opened.activeSelf){
        //Open door
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(openRot), Time.deltaTime * smooth);
    }
 
    if(closed.activeSelf){
        //Close door
        transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.Euler(defaultRot), Time.deltaTime * smooth);
    }

That’s for minimal changes to your code. If the doors still rotate wrong, I’d suggest you only store a float for the angle of how much the door has opened, and set the rotation with that.

1 Like