Search Unity

Question How can I move my camera?

Discussion in '2D' started by tomlugin100, Jun 1, 2023.

  1. tomlugin100

    tomlugin100

    Joined:
    May 6, 2023
    Posts:
    84
    Hello everyone! Thank you for reading my post. I'm building a 2d game and I'm working with a canvas. I want to view the canvas differently (I want to follow my jumping red jelly with the camera) but when I change the position of my orthographic camera, nothing changes in how the game looks. I still am looking at the entire canvas, instead of part of it. How can I move my camera so that the canvas is partly cut off from the view? As you can see, the camera is to the left of the view, at the same height as the red square jelly. I want to raise the camera's viewport when the jelly jumps so that I can follow the jelly as it jumps.

    I have this code in the jelly's script:
    Code (CSharp):
    1. if (target.position.y > transform.position.y) //- CanvasScripts.screenHeight / 6)
    2.         {
    3.             transform.position = new Vector3(transform.position.x, target.position.y, transform.position.z);
    4.         }
    where target is the jelly's transform

    Because of this code I can see the camera rising along the left of the screen but everything still looks the same.

    Before, when working on another project without a canvas the camera code worked, so I'm wondering why the canvas changes that
     

    Attached Files:

    Last edited: Jun 1, 2023
  2. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    canvas always follows the camera, use sprite renderer
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,744
    Camera stuff is pretty tricky... you may wish to consider using Cinemachine from the Unity Package Manager.

    There's even a dedicated forum: https://forum.unity.com/forums/cinemachine.136/

    Otherwise go check out tutorials for games that have similar camera work to what you contemplate.