Search Unity

Question Centering an *angled* camera on a gameobject

Discussion in 'Scripting' started by TheSojourner, Nov 27, 2022.

  1. TheSojourner

    TheSojourner

    Joined:
    Jul 13, 2018
    Posts:
    5
    Heya folk. Coming here with an issue that's probably as old as time.

    I moved recently from an entirely 2d-based project to 3d, and while it's been smooth sailing for the most part, there are some hurdles with the camera that have been giving me a headache lately.

    My ortographic camera is angled (it was easier to angle the camera instead of angling all my objects), rotated by 25x 7.5y, standing at a distance of -200z units. Give or take.
    I can move the camera just fine translating its position using WASD inputs, however:

    I want to center the camera on an object in my mostly 2d plane, but the inclination means that I'm also working with the Z position of the camera and I can't just say "lol camera position = object position".

    Example of camera manually centered on an object.
    https://i.imgur.com/f5mkEC0.png
    And the values of that object
    https://i.imgur.com/KinO9I0.png
    Note the object's Z value never changes. They move in a X Y grid.

    Any idea how I could get those numbers? Thanks a bunch.
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,113
    You don't need any numbers. Nest the camera inside the object that is supposed to be centered, call that object "PIVOT". Move that child camera -200 Z locally (and adjust its local rotations to taste). Now rotate PIVOT around Y. It's called pivoting.

    Feel free to reposition PIVOT anywhere you want. Camera will always keep PIVOT wherever you've placed it, and maintain own distance and orientation.
     
    TheSojourner likes this.
  3. TheSojourner

    TheSojourner

    Joined:
    Jul 13, 2018
    Posts:
    5
    I'm not sure how this could help me in this instance.
    A pivot would be great if I wanted to rotate the camera around an object.

    What I want is to move my camera to land center view on any possible object, like you would do in any top-down-view strategy game. The issue lies again in that what we perceive the center is not mathematically the center.
    Screeny example:
    https://i.imgur.com/AoR58w9.png
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,743
    TheSojourner likes this.
  5. TheSojourner

    TheSojourner

    Joined:
    Jul 13, 2018
    Posts:
    5
    Cinemashine seems great, but I don't want to overly complicate myself or bloat the project with larger tools.

    If this was an adventure project with dramatic shots, scenes and effects all over the place, I'd give my players motion sickness with every trick I could use, but this is just a regular strategy game with an ortographic camera. I just need it to perform the most basic functions of zoom in, zoom out, move around. And this.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,743
    Then start with some tutorials, as you have posted exactly zero code so far. No sense in one of us laboriously trying to type it all again for you in this little tiny box.

    Screen Shot 2022-11-27 at 6.50.09 AM.png

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

    Finally, when you have errors, don't post here... just go fix your errors! Here's how:

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly?

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.
     
  7. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,444
    It sounds like you are thinking of the everyday meaning of the word "pivot", not the Unity meaning. Their suggestion is spot on. The Unity "pivot" is essentially just a convenient position in space which helps you achieve a relative offset, whether *rotational* or *translational*.

    Create an Empty. Put the Empty on your 2D plane where you want to look, the exact spot you want "visually centered."

    Move your Camera to anywhere else, framing up your view exactly where you think looks best and "visually centers" the subject on your 2D plane.

    In the Hierarchy, drop your Camera onto the Empty so it becomes a child of the Empty.

    In the editor, drag your Empty anywhere around the 2D plane. The camera will follow, and more importantly, will maintain the angle that keeps the Empty "visually centered." No rotation needed. It maintains the framing you set up.
     
    orionsyndrome and TheSojourner like this.
  8. TheSojourner

    TheSojourner

    Joined:
    Jul 13, 2018
    Posts:
    5
    A good observation; I thought them equivalent.
    I'll give this another stab after I've brutalized the sack/hay/mat.
     
  9. TheSojourner

    TheSojourner

    Joined:
    Jul 13, 2018
    Posts:
    5
    It took me a bit of fiddling with the transforms.
    I ended up rotating the newly created pivot instead of the camera, and I slammed the pivot into the hierarchy along the objects it is meant to track to share local coordinates.

    Somehow, this worked. There's some kludge involved in my solution, but I don't plan to touch the camera again for the lifespan of this project, so that's good enough for me.
    Thank you so much for the help.
     
  10. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,113
    You don't really understand how transforms work, do you?