Search Unity

Rotating an object to look from left to right and vice versa

Discussion in 'Scripting' started by fernandortiz7, Mar 26, 2015.

  1. fernandortiz7

    fernandortiz7

    Joined:
    Mar 9, 2015
    Posts:
    1
    What I'm trying to do is to rotate a 3d object to look right when is moved positive on the z axis and to make it look left when is moved negative on the z axis. I written this code (h being the Input.GetAxis ("Horizontal") and declaring playerLookingRight = true on the Start function):

    void Turning(float h){
    float tempX = transform.rotation.x;
    float tempZ = transform.rotation.z;
    float tempW = transform.rotation.w;

    if (playerLookingRight && h < 0) {
    playerRigidbody.transform.rotation = new Quaternion(tempX, 0.0f , tempZ, tempW);
    playerLookingRight = false;
    Debug.Log("Goes Left");
    }
    if (!playerLookingRight && h > 0) {
    playerRigidbody.transform.rotation = new Quaternion(tempX, 0.0f , tempZ, tempW);
    playerLookingRight = true;
    Debug.Log("Goes Right");
    }
    }

    The code works to look left once but it wont turn back to right. The ifs are working cause the Debugs work as expected.
     
  2. lineupthesky

    lineupthesky

    Joined:
    Jan 31, 2015
    Posts:
    92
    I can tell you that never ever directly edit the rotation, unless you have a deep knowledge on Quaternions. Rotation in Unity is something to do with the Quaternions, imaginary and real numbers. You can have a look at how they work over wikipedia page, here. I can't give you a direct help unless the controls you want is given clearly. But I can guide you to some references. To rotate an object, you can use couple of things :


    All of these serve for the same thing, but use a different approach to do the job. You can have a look at the examples in the reference pages or over the internet to see how and where they are used.

    Also, If I am to give couple of little examples, using eulerAngles in order to rotate an object is a very good approach. It's a Vector3 variable, and you can simply use Vector3.Lerp to linearly interpolate your object's rotation to the desired amount.

    Code (CSharp):
    1. transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, new Vector3(0,30,0), Time.deltaTime * 15);
    Also, Quaternion Lerp is an another thing that you can use, you can simply lerp a rotation to another one, like this :

    Code (CSharp):
    1. transform.rotation = Quaternion.Lerp(transform.rotation, target.rotation, Time.deltaTime * 15);
    But if you are going to change any value in that target rotation, like maybe you want to rotate only on Y axis, then I suggest you to use Quaternion.Euler for that. It simply takes the Vector3 variable, and transforms it into a Quaternion.

    Code (CSharp):
    1. transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(new Vector3(transform.eulerAngles.x, target.eulerAngles.y, transform.eulerAngles.z)), Time.deltaTime * 15 );
    Also you can combine the methods I've given above, and create the desired rotation. In your case, I suggest AngleAxis or simply lerping the EulerAngles.

    PS, you can do these rotation changes in every update, if you want your object to be rotated all the time, but if you want your object to rotate after finishing the last rotation, like rotating to fully left, then full right, etc, you can always use Coroutines. They are your friend and will help you a lot, both in script management and optimization, also in mobility.

    PS, using Lerp & Slerp functions make your rotations to happen "over time" so, if you want direct change, you can simply edit eulerAngles of your object. Simple approach for your problem :
    Code (CSharp):
    1.  
    2. if(Input.GetAxis("Horizontal") < 0)
    3. {
    4. transform.eulerAngles = new Vector3(tempX, 0, tempZ);
    5. // ------ OR ------
    6. // transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, new Vector3(tempX, 0, tempZ), Time.deltaTime * speed);
    7. }
    and the same logic goes for the other one. But note that if you connect your "if" logic to an Input directly, your rotation might be very laggy because Inputs are too precise. ( Unless you want your rotation to happen directly, not over time. )