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
1
Question by Xatoku · Jun 04, 2011 at 07:47 PM · camera2dgamezoomfighting

Fighting Game Camera

I have an idea on how to make a 2d fighting game camera, but no idea how to implement it. I basically want the camera to focus on 2 Fighters and zoom in/out to make sure they're both on screen. Id imagine I would have to find the middle between these objects and then move the camera from there but I'm stumped on how to do so. Any help is greatly 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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by aldonaletto · Jun 04, 2011 at 11:09 PM

Supposing you will be using X and Y axis, I think you must define continuously the left and right limits (xL and xR) of your scene based on each fighter's position - let a margin of 1 or 2 meters behind each fighter, for instance - then alter the Z coordinate of the camera in order to get the borders at these points. Since the camera's view angle and the players plane form a triangle, the scene width will be proportional to the distance of the camera to the plane. You can set manually this distance at design time - let's call it zCam, and wScene the initial width of the scene. During the fight, you can set the distance of the camera to zCam*(xR-xL)/wScene, and the camera's X coordinate to the center of the scene, (xR+xL)/2.
Hope this algorithm works for you!

SCRIPTS ADDED: Create a new project; create the two fighters (simple capsules, for instance), name them Fighter1 and Fighter2 and set their Z and Y coordinates to 0. Add the script below to each one, and modify the Fighter2 command keys in the Inspector to "," and ".", for instance.

 private var step:float = 5;
 var leftKey = "z";
 var rightKey = "x";
 
 function Update(){
 
     if (Input.GetKey(leftKey)) 
         transform.position.x -= step*Time.deltaTime;
     if (Input.GetKey(rightKey)) 
         transform.position.x += step*Time.deltaTime;
 }

Add the script below to the Main Camera:

 var margin:float = 1.5; // space between screen border and nearest fighter
 
 private var z0:float = 0; // coord z of the fighters plane
 private var zCam:float; // camera distance to the fighters plane
 private var wScene:float; // scene width
 private var f1:Transform; // fighter1 transform
 private var f2:Transform; // fighter2 transform
 private var xL:float; // left screen X coordinate
 private var xR:float; // right screen X coordinate
 
 function calcScreen(p1:Transform, p2:Transform){
     // 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;
     }
 }
 
 function 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;
 }
 
 function Update(){
 
     calcScreen(f1,f2);
     var width:float = xR-xL;
     if (width>wScene){ // if fighters too far adjust camera distance
         transform.position.z = zCam*width/wScene+z0;
     }
     // centers the camera
     transform.position.x = (xR+xL)/2;
 }

This script follows the fighters. If they fit in the scene, the camera just centers itself. It they get too far, the camera zooms out in order to catch the entire scene.

Comment
Add comment · Show 6 · Share
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
avatar image Xatoku · Jun 05, 2011 at 12:41 AM 0
Share

Alright I'll probably need a js example to fully understand but basically I find how far each fighter is from the border, subtract the 2 numbers, and multiply the sum by the camera's Z co-ordinate at all times and then move the camera based on the distance each fighter is from the border when added & divided by 2, giving me the midpoint between each character which the camera then follows? Like I said, I think I understand it but I'm not sure how to translate it to javascript.

avatar image aldonaletto · Jun 05, 2011 at 12:47 AM 0
Share

I'll try to create something more specific. I'll be back soon on this.

avatar image aldonaletto · Jun 05, 2011 at 02:43 AM 0
Share

I created a simple project in order to test the algorithm, and attached the scripts to my previous answer. Hope they can be a good start for your game.

avatar image yashpal · Jan 24, 2015 at 12:13 PM 0
Share

Thanks @aldonaletto it working great. I just add lerp to move camera more smoothly.

avatar image pixelone · Jul 15, 2015 at 05:22 AM 0
Share

Thank you for the sharing this. How exactly did you smooth the camera zoom with Lerp? Thanks

Show more comments
avatar image
0

Answer by Maynk · Feb 11, 2021 at 04:32 PM

Hey You Check This

Setting up Fighting Game Camera Using Cinemachine in Unity3D

Comment
Add comment · Share
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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

ortographic camera smooth follow with zoom 1 Answer

2D camera zoom in comparison to the height of target 2 Answers

2D camera zoom smoothing and limitations? 1 Answer

Zoom Pinch 1 Answer

Why does the camera zoom out when maximized on play? 0 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