0
\$\begingroup\$

Hello i have 2d mobile game where camera follow players car, on my mobile phone with resolution 2960x1440 everything works fine but when i start the game on tablet with resolution 2160x1620 the camera shows only a half of the car... i made some drawings for better imagination :)

first is my mobile resolution

and second is my tablet resolution

I was trying to do it by myself for a few days but i am new in unity and i do not fully understand Camera class

i have this script on my camera:

public GameObject playerPos;
private Vector3 lastPlayerPosition;
private float distanceToMove;
void Start()
{
    lastPlayerPosition = playerPos.transform.position;
}
void Update()
{
    
        distanceToMove = playerPos.transform.position.x - lastPlayerPosition.x;
        transform.position = new Vector3(transform.position.x + distanceToMove,         transform.position.y, transform.position.z);
        lastPlayerPosition = playerPos.transform.position;
         
}
\$\endgroup\$
5
  • \$\begingroup\$ So you want the center of the car to be always at "1/3 from the left" of your screen? \$\endgroup\$ – Vaillancourt Jul 21 at 17:12
  • \$\begingroup\$ yes 1/4 would be best \$\endgroup\$ – Cpt Teemo Jul 21 at 17:49
  • \$\begingroup\$ You'll need to edit your question to explain how you have implemented the camera following behaviour. Do you have a script on your camera that is moving it? Or is the camera parented to the car's transform? Or some other strategy? You should aim so that a reader could reproduce exactly the same problem in a new empty project using only the information and code shown in your question. \$\endgroup\$ – DMGregory Jul 21 at 18:41
  • \$\begingroup\$ Going by your illustrations, it looks like it's a problem with aspect ratio. I suspect you're setting the camera's horizontal distance from the car working off the screen's height, which is why the car's clipped from view. You can get the current aspect ratio by dividing Screen.width by Screen.height and factor that into your calculations. It's hard to say more without seeing your code, though. \$\endgroup\$ – verified_tinker Jul 21 at 19:08
  • \$\begingroup\$ Please note that Answers should be used to post answers to the question, not to provide additional details. To do this, you need to edit the question. \$\endgroup\$ – Vaillancourt Jul 21 at 19:56
0
\$\begingroup\$

In unity, you can use Cinemachine to define a region on the screen and the camera will move to keep your object inside that region. I think the Framing Transposer is the behaviour you're looking for.

See their tutorial or install the package and install one of their samples for an interactive demo.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.