Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Zombiariel · May 18, 2020 at 06:01 AM · cameraunity 2dcamera-movementcamera followfightingame

Unity 2D Fighting Game Camera

I'm trying to make a 2d fighting camera. I want the camera to focus on 2 fighters and then zoom in/out to make sure they are both on the screen. Using the post below, I was able to get this camera to work horizontally:

https://answers.unity.com/questions/126184/fighting-game-camera.html

The original script is the following

 float margin = 1.5f; // space between screen border and nearest fighter    
  private float z0; // coord z of the fighters plane
  private float zCam; // camera distance to the fighters plane
  private float wScene; // scene width
  private Transform f1; // fighter1 transform
  private Transform f2; // fighter2 transform
  private float xL; // left screen X coordinate
  private float xR; // right screen X coordinate
 
     public void calcScreen(Transform p1, Transform p2)
     {
         // Calculates the xL and xR screen coordinates 
         if (p1.position.x < p2.position.x)
         {
             xL = p1.position.x - margin;
             xR = p2.position.x + margin;
         }
         else
         {
             xL = p2.position.x - margin;
             xR = p1.position.x + margin;
         }
 
     }
 
     public void Start()
     {
         // find references to the fighters
         f1 = GameObject.Find("Fighter1").transform;
         f2 = GameObject.Find("Fighter2").transform;
         // initializes scene size and camera distance
         calcScreen(f1, f2);
         wScene = xR - xL;
         zCam = transform.position.z - z0;
     }
 
     public void Update()
     {
         Vector3 pos = transform.position;
         calcScreen(f1, f2);
         float width = xR - xL;
     
         if (width > wScene)
         { // if fighters too far adjust camera distance
             pos.z = zCam * width / wScene + z0;
         }
 
         // centers the camera
         pos.x = (xR + xL) / 2;    
 
         transform.position = pos;
     }

Since on my game you can move horizontally and vertically, I modified the code to also adjust when the character moves vertically to this:

 float margin = 1.5f; // space between screen border and nearest fighter
 float marginY = 1.5f; // space between screen border and nearest fighter
 
  private float z0; // coord z of the fighters plane
  private float zCam; // camera distance to the fighters plane
  private float wScene; // scene width
  private float hScene; // scene height
  private Transform f1; // fighter1 transform
  private Transform f2; // fighter2 transform
  private float xL; // left screen X coordinate
  private float xR; // right screen X coordinate
  private float yU; // up screen Y coordinate
  private float yD; // down screen Y coordinate
 
     public void calcScreen(Transform p1, Transform p2)
     {
         // Calculates the xL and xR screen coordinates 
         if (p1.position.x < p2.position.x)
         {
         xL = p1.position.x - margin;
         xR = p2.position.x + margin;
     }
     else
     {
         xL = p2.position.x - margin;
         xR = p1.position.x + margin;
     }

     if (p1.position.y < p2.position.y)
     {
         yD = p1.position.y - marginY;
         yU = p2.position.y + marginY;
     }
     else
     {
         yD = p2.position.y - marginY;
         yU = p1.position.y + marginY;
     }

 }

 public void Start()
 {
     // find references to the fighters
     f1 = GameObject.Find("Fighter1").transform;
     f2 = GameObject.Find("Fighter2").transform;
     // initializes scene size and camera distance
     calcScreen(f1, f2);
     wScene = xR - xL;
     hScene = yU - yD;
     zCam = transform.position.z - z0;
 }

 public void Update()
 {
     Vector3 pos = transform.position;
     calcScreen(f1, f2);
     float width = xR - xL;
     float height = yU - yD;

     if (width > wScene)
     { // if fighters too far adjust camera distance
         pos.z = zCam * width / wScene + z0;
     }
     else if (height > hScene)
     { // if fighters too far adjust camera distance
         pos.z = zCam * height / hScene + z0;
     }

     // centers the camera
     pos.x = (xR + xL) / 2;
     pos.y = (yU + yD) / 2;


     transform.position = pos;
 }

This works when the characters only moves horizontally or vertically, if it moves both horizontally and vertically it doesnt work anymore. Also, the vertical movement doesnt work appropiately when the 2 fighters are far way. The camera zooms in even thought they are far from each other but they are close in therms of the height.

I feel like I need to change the code to look at both x and y at the same time. It feels like I'm considering and x separately.

I have been stuck on this for a while now. Any help would be appreciated.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

204 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Follow camera in 2d game 2 Answers

Cinemachine Confiner 2D shift my camera view 0 Answers

Keeping UI Element To Game Object When Camera Moves 2 Answers

Smooth camera script looking at left hand side of character. 0 Answers

My game is laggy, but there are only a ball and some flat platforms. 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges