简体   繁体   中英

How can I retain player orientation after a button is released in C# on Unity3D?

I'm trying to flip a 3D character to get it to face the correct direction that is pressed. So far, my code is working:

    private void OrientAvatar()
{
    if (ControlVector.x > DeadZone)
    {
        Spin = -90;
    }

    if (ControlVector.x < DeadZone)
    {

        Spin = 90;
    }

    if (ControlVector.y > DeadZone)
    {

        Spin = 180;
    }

    Pivot.eulerAngles = new Vector3(Pivot.rotation.x, Spin, Pivot.rotation.z);  
}

However, when the keys are released it'll flip direction to the opposite. How can I retain the previous orientation set so that the character model remains facing in the correct direction?

I figured it out with a few minor code corrections:

private void OrientAvatar()
{
    Debug.Log(ControlVector.x);

    if (ControlVector.x >= DeadZone)
    {
        Spin = -90;
    }

    if (ControlVector.x <= -DeadZone)
    {
        Spin = 90;

    }

    if (ControlVector.y > DeadZone && Ladder)
    {
        Spin = 180;
    }

    Pivot.eulerAngles = new Vector3(Pivot.rotation.x, Spin, Pivot.rotation.z);  
}

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