Search Unity

[Closed] Need Math Formula Perspective Camera

Discussion in 'Scripting' started by coute, Mar 15, 2018.

  1. coute

    coute

    Joined:
    Jul 26, 2014
    Posts:
    9
    I have a perspective camera that's has a 30 x rotation. It zooms in and out by moving in the z and y axis (y to keep the ground on the bottom of the screen)
    .
    I need a math formula for how much to move the background elements to match with the foreground.

    This is kinda hard to explain so I made a YT video to show the problem:


    As you can see, the mountains move too much. If it were a tree instead, the top of the tree would move under the ground as I zoom in. I created a basic formula that moves background elements at a fixed rate but I could not get it to match exactly. This probably requires some sin/cos/tan stuff and I don't know how to find out the formula.

    This formula would need the distance the background object is from the camera as a variable because how fast the background moves up and down will depend on how far it is from the camera.

    I came up with this from a formula I found online:

    dist = Vector3.Distance(backDrop.transform.position, transform.position);
    height = 2 * Mathf.Tan(20) * dist;

    From what I read, this gives the visible height of a perspective camera. I feel like I can use this formula some how but I've been messing with it for a long time and I'm not sure how to use it / how it works into my problem.

    I also have this line:

    backDrop.transform.position -= transform.up * _zoomAmount * ???????????;
    Which I'm using to move the background picture. I've been changing the ??? part to try and find something that would match up using trial and error but it's been a nightmare. I need the _zoomAmount variable so that the background only moves when the camera is moving.

    Please help, I've been messing with just the camera in my game for 2 whole months. I finally decided that I just need to ask somebody because I'm stuck.

    I'm still new to Unity so please make you explanation as simple as possible. Thanks in advance.
     
  2. jam-slc

    jam-slc

    Joined:
    Oct 18, 2016
    Posts:
    9
    I may be over-simplifying but can't you just set the backdrop position to the camera position multiplied by an amount?

    Code (CSharp):
    1. public float backdropScrollSpeed = 0.5f;
    2.  
    3. // in Update or LateUpdate:
    4. backDrop.transform.position = transform.position * backdropScrollSpeed;
    Setting backdropScrollSpeed to 1.0 would match the camera 1:1 like a skybox, closer to 0 would move it less...

    That would move on all 3 axis, if you want only height you can set to Y only like:

    Code (CSharp):
    1. backDrop.transform.position = new Vector3(transform.position.x, transform.position.y * backdropScrollSpeed, transform.position.z);
     
  3. coute

    coute

    Joined:
    Jul 26, 2014
    Posts:
    9
    I tried your idea. What happened is that the backdrop moved in the same direction as the camera when they need to move in the opposite directions. When zooming out, the camera moves up and the backdrop also goes up when it should go down because the foreground is taking up less space.

    Do you know how to make the backDrop go down as the camera is going up?

    Also, if possible, I would have liked the backDrop to move in it's up axis instead of straight up since it is slanted at a 30 degree angle but once I can get it to move in the opposite direction I'll see if it matches up and looks okay. Moving the backdrop straight up in the editor and watching the game view looks weird though due to the slant.

    EDIT: Okay, I figured out how to get it to move down when the camera moves up and vice vera using the difference between the camera's previous y and it's current y. Sorry to say that the backdrop still does not match up (even when I used this method to move the backdrop in it's up axis, which I also tried): even if it's okay in the beginning, the backdrop soon goes way off. Like I thought originally: no constant value is going to make it match up. The multiplier has to be a function.

    EDITEDIT: Long story short, I constructed an equation using cosin that works well (also a log function works almost as well). But I realized that this function doesn't take into account the case that the camera moves up while not zooming. I could have this never happen in the game but that's a little limiting. Accounting for this would require another variable (3d function) so I decided that I've given up trying to fight the camera and will use another solution.
     
    Last edited: Mar 16, 2018