Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How do I code this camera?

Discussion in 'Getting Started' started by Gummi_Ghost, Oct 22, 2021.

  1. Gummi_Ghost

    Gummi_Ghost

    Joined:
    Sep 21, 2020
    Posts:
    35
    I've read the documentation for the Camera, but still don't understand. The 3 tutorials I've followed, including this one, have the player at the center of the screen when the camera catches up. I want to code the camera like this:

    The player can move anywhere on screen with no gravity (Completed). If the player goes very close to the right wall, the camera slowly "unlocks" more right screen. The left of the screen does not do this; If the player moves all the way left, he hits a wall. Once the player and camera move right, there's no accessing the old screen to the left.

    The closest example I can think of for the camera is like a 2d runner game, but the player can move anywhere on screen without gravity. I wouldn't ask someone to code this unless they wanted, so if there are any references or tutorials you could link I would greatly appreciate it.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    I don't know of a tutorial. But you can do this yourself, step by step.
    1. Make an empty CameraController script, and attach it to your camera. Make sure that runs (though it does nothing).
    2. In its Update method, write some code to figure out where the player is on screen. You could do this using Camera.WorldToScreenPoint, for example. Your script will need a reference to the player transform. When you've calculated the screen position of the player, simply Debug.Log it. Run and confirm that those log messages are what you expect.
    3. Now add an "if" statement so that it logs only when the player is close to the right side of the screen (however you define "close"). Run and test.
    4. Now, inside that if statement, shift the camera's own transform to the right slowly. Something like transform.position += 0.5 * Time.deltaTime might do. Run and test.
    5. Refine that motion until it feels just right.
    If you have trouble with any of these steps, post here and get some help. Good luck!
     
    Gummi_Ghost likes this.
  3. Gummi_Ghost

    Gummi_Ghost

    Joined:
    Sep 21, 2020
    Posts:
    35
    @JoeStrout Thank you so much! I will give it a go!
     
    JoeStrout likes this.
  4. Gummi_Ghost

    Gummi_Ghost

    Joined:
    Sep 21, 2020
    Posts:
    35
    @JoeStrout, hi, I just wanted to say that your advice did help, and I was able to make it work!
     
    JoeStrout likes this.