1
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;

public class RotateObject : MonoBehaviour
{
    public UnityStandardAssets.ImageEffects.Blur blur;
    public UnityStandardAssets.ImageEffects.BlurOptimized blurOptimized;
    public FadeScript fadescript;
    [SerializeField] Quaternion targetRotation;    /// <summary>Rotation you want to achieve.</summary>
    [SerializeField] private float s;       /// <summary>Rotation duration.</summary>
    private Quaternion initialRotation;     /// <summary>Your GameObject's initial rotation.</summary>
    private float t;                        /// <summary>Your 't' reference.</summary>
    private FirstPersonController fpcscript;


    void Awake()
    {
        fpcscript.enabled = false;
        fpcscript = transform.GetComponent<FirstPersonController>();
        initialRotation = transform.localRotation;
    }

    void Update()
    {
        if (fadescript.alphaZero == true)
        {
            if (t < 1.0f)
            {
                transform.rotation = Quaternion.Lerp(initialRotation, targetRotation, t);
                t += (Time.deltaTime / s); /// Each 60 frames [optimally] t reaches 1, divided by your duration, it will reach 1 on 'x' seconds.
            }
            else /// Your rotation ends here:
            { /// reset 't' [if you please], and reset 'initialRotation' 
                initialRotation = transform.rotation;

                blur.enabled = false;
                blurOptimized.enabled = false;
                fpcscript.enabled = true;
            }
        }
    }
}

The script is attached to FPSController. In the Inspector the Rotation of the FPSController is: X = 7 Y = 112 Z = -31

But when i'm using a break point on the line in the Awake:

initialRotation = transform.localRotation;

I see that initialRotation values are: (-0.2, 0.8, -0.2, 0.5)

What i want to do is to rotate the transofrm from X = 7 Y = 112 Z = -31 to rotation 0,0,0 That is why i also didn't set any values to targetRotation since i want it to rotate to 0,0,0

\$\endgroup\$
2
\$\begingroup\$

The values that the Log throws you are of a Quaternion, which is a matrix of 4 components { x, y, z, w }, if you would like to Log your Quaternion in an Euler (x, y, z) format, Log transform.eulerAngles instead.

For the next point, if you want to set the rotation to Euler(0, 0, 0) you can either:

  • Use Quaternion.Euler(0f, 0f, 0f);
  • Use Quaternion.identity

And assign that rotation to the Lerp.

    void Awake()
    {
        fpcscript.enabled = false;
        fpcscript = transform.GetComponent<FirstPersonController>();
        initialRotation = transform.localRotation;

        Debug.Log("Rotation on Euler format is: " + transform.eulerAngles);
    }

    void Update()
    {
        if (fadescript.alphaZero == true)
        {
            if (t < 1.0f)
            {
                transform.rotation = Quaternion.Lerp(initialRotation, Quaternion.identity, t);
                t += (Time.deltaTime / s); /// Each 60 frames [optimally] t reaches 1, divided by your duration, it will reach 1 on 'x' seconds.
            }
            else /// Your rotation ends here, so you reached Euler(0, 0, 0).
            { /// reset 't' [if you please], and reset 'initialRotation' 
                initialRotation = transform.rotation;

                blur.enabled = false;
                blurOptimized.enabled = false;
                fpcscript.enabled = true;
            }
        }
    }
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.