Search Unity

How can I make my 2D camera and world rotate around its y-axis?

Discussion in '2D' started by Doh09, Feb 18, 2016.

  1. Doh09

    Doh09

    Joined:
    Jan 12, 2015
    Posts:
    16
    I have built a basic 2D endless runner game where you run on platforms, jump and double jump.

    What I want to do is to make it so I run in circles along the y axis, to avoid getting an out of bounds issue if I ever reach the float number boundary.

    To ensure this runs correctly, I would need all objects in the world to keep the same rotation, including the camera.

    This however, presents an issue for me as I'm not sure how to solve it.

    I have tried adding the following 2 scripts to the MainScene Scene Object I have, but none of them works, either only the player rotates or the whole world rotates but the camera doesn't and the player falls down because of the rotation.

    Script 1:
    Code (csharp):
    1.  
    2.     public Transform target;
    3.  
    4.     // Update is called once per frame
    5.     void Update () {
    6.         transform.rotation = Quaternion.Euler(transform.rotation.x, target.transform.rotation.y, transform.rotation.z); //lock y-rotation on target
    7.     }
    8.  
    Script 2:
    Code (csharp):
    1.  
    2.     void FixedUpdate () {
    3.         if (playerScript.playerIsMoving)
    4.         {
    5.             //rotate to keep within float boundaries of the world, but only if the player is moving.
    6.             transform.Rotate(transform.rotation.x, transform.rotation.y + 0.1f, transform.rotation.z);
    7.         }
    8.     }
    9.  
    Any ideas?

    Thank you for reading this and your help.
     
  2. Doh09

    Doh09

    Joined:
    Jan 12, 2015
    Posts:
    16
    Ok, I've made the world and camera rotate with this code.

    Code (csharp):
    1.  
    2.             if (playerScript.playerIsMoving)
    3.             {
    4.                 //rotate to keep within float boundaries of the world, but only if the player is moving.
    5.                 Quaternion camPosition = transform.rotation;
    6.                 camPosition.eulerAngles = Vector3.Lerp(camPosition.eulerAngles, new Vector3(camPosition.eulerAngles.x, camPosition.eulerAngles.y + 0.1f, camPosition.eulerAngles.z), Time.deltaTime);
    7.                 transform.rotation = camPosition;
    8.             }
    9.  
    This however, does not seem to work as intended.

    What happens is this.

    Here's what it normally looks like.
    0rotation.png

    Here's when I only rotated the scene.
    mainscene180rotation.png

    Here's when I rotated the camera AND the scene. both180rotation.png

    Why doesn't this work?
     
  3. Doh09

    Doh09

    Joined:
    Jan 12, 2015
    Posts:
    16
    As far as I can gather, the issue comes down to sprites by default only being visible from 1 side.
     
  4. Doh09

    Doh09

    Joined:
    Jan 12, 2015
    Posts:
    16
    I've stopped working on this issue for now, as I believe this requires a 3d world and not a 2d.

    So i'll just live with potentially having the float boundary issue, unless someone knows a solution to that.