خرید بک لینک

Vote count: 0

I'm making a space game, and to pitch a spaceship, you have to click on a UI 'dial' (circle) to set the desired 'pitch' angle (IE you click ~45degrees above the x axis on a circle to pitch upwards by 45 degrees etc)

The same for rotating left and right, and rolling left and right. Rotating and rolling both work fine as intended.

However, when I attempt to pitch (all using the same mathematics/code) it works fine for the first +89 and the first -89 degrees (89 degrees up and 89 degrees down), but after that, it seems to "lock" at +/-90 degrees.

It works by deccrementing the .x value of a vector3 variable each frame. It appears (from what I can tell) that when the angle reaches + or - 90degrees, it sets the y and z components to 180 (flipping the object), however continues to decrement (which obviously just send it the other way). I'm sure I could work out a method to, when the components are set to 180, to start incrementing instead of decrementing etc, but I was hoping if there was an easier/cleaner way (or if I'm missing something/doing somethign wrong in my code?)

below is my code;

private void FixedUpdate()
{
    //Boolean, this is set to true when the 'circle' (dial) is pressed
    if (pitchChanged)
    {
        //This script continues to run when the "handle" gameobject (Dial) does not exist, allows the ship to continue rotating when the player is not interacting with the dial(s)
        if (handle != null)
        {
            Vector3 euAngle = handle.transform.eulerAngles;
            currAngle = euAngle;
        }


        //The current angle of the spaceship
        Vector3 shipAngle = shipController.gameObject.transform.eulerAngles;



        //Decrement the x axis by a set rate (rotationSpeed = 0.75f)
        shipAngle.x -= rotationSpeed;
        currAngle.z += rotationSpeed;



        //Calculate the difference between currentAngle of the dial (it happens gradually, not instantly) and the user's desired angle
        float currentAngleDifference = currAngle.z - userTargetAngle;


        //Set/Update the ship's angle 
        shipController.gameObject.transform.eulerAngles = shipAngle;



        //Again if the user is interacting with the dials (thus handle exists) then update the handle/dial rotation too
        if (handle != null)
        {
            handle.transform.eulerAngles = currAngle;
        }



        //If the difference between the angles is within a certain range, then stop rotating/pitching and snap the rotation/pitch of the ship (and dial) to the desired position (prevents the dial from skipping past the intended angle)
        if (currentAngleDifference <= 1.5f && currentAngleDifference >= -1.5f)
        {
            pitchChanged = false;
            currAngle.z = userTargetAngle;
            shipAngle.x = -userTargetAngle;
            shipController.gameObject.transform.eulerAngles = shipAngle;
            if (handle != null)
            {
                handle.transform.eulerAngles = currAngle;
            }
        }
    }
}

asked 48 secs ago

برچسب: نویسنده: استخدام کار تاريخ: پنجشنبه 14 مرداد 1395 ساعت: 8:00

صفحه بندی