Search Unity

Need some help with camera movement, probably need a math wiz.

Discussion in 'Scripting' started by Sor, Jan 12, 2014.

  1. Sor

    Sor

    Joined:
    Sep 25, 2013
    Posts:
    19
    OK, so I'm at my wits end with my math skills and probably understanding of the issue I am having.

    I have a draggable 2D sprite on a 2D ortho cam that is positioned below a ship that you can drag the ship around the screen. Which all works perfect.

    I made a change that will also pan the camera to allow for more usable space on the X axis. So, when you drag left on the screen (negative X axis), I lerp the ship (works just fine), and also tried to do the same with the 3d camera to allow for a panning effect.

    The player can move from -200 <-> +200 on the X axis. The camera pans as this happens, but the camera is limited in X movement to the extents of the drawable area when the player is at Vector3.zero. The effect (in the below code) is working, but the draggable 2d sprite and the ship are slightly un-aligned (more-so at the edges of the screen). The camera is at Y 350, ship is at 0.

    Can anyone please show me how to handle the conversion to move the camera when dragging but keep the ship and drag sprites aligned?

    void SetPlayerPosition() {

    // Move the ship to follow this object (targetToMove is the 2d sprite / drag handle)

    pos = GameController.uiCamera.WorldToViewportPoint( targetToMove.transform.position );
    pos.z = GameController.gameCamera.transform.position.y;
    pos = GameController.gameCamera.ViewportToWorldPoint ( pos );

    // This is the camera that we want to pan on the X axis
    tVector3 = GameController.gameCamera.transform.position;

    // SHIP
    pos.x = Mathf.Lerp(-200f, 200f, (targetToMove.position.x + (-maxL) ) / ( (-maxL) + maxR) );

    // CAMERA
    tVector3.x = Mathf.Lerp(
    GameController.playerMaxL,
    GameController.playerMaxR,
    (targetToMove.position.x + (-maxL) ) / ( (-maxL) + maxR) );

    GameController.playerObject.position = pos;
    GameController.gameCamera.transform.position = tVector3;
    }
     
  2. Sor

    Sor

    Joined:
    Sep 25, 2013
    Posts:
    19
    This is solved, thank you all. I was overcomplicating the issue and the solution was very easy.