Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 DevMerlin · Nov 10, 2014 at 12:16 AM · c#camerapositionorthographiclimits

How can I keep three targets in sight, and keep the camera above a specific height - while it resizes?

I have a 2D Orthographic camera for a game that is built similarly to Tennis. I've been working out the code for this, and I think I'm only part of the way so far. At the moment... it... sort-of keeps the two players in frame, with the frame leading more towards the human player - Player One. However, it also has a tendency to drop below the ground level - especially when it zooms out to keep the targets in view. There are three specific targets: The Ball, Player One, and Player Two. I'm probably way off, but this is my existing code:

 using UnityEngine;
 using System.Collections;
 
 public class CameraController : MonoBehaviour {
 
     // Moving Camera, This Camera tries to follow Player One, Two, and Ball //
 
 public static CameraController Instance;
 public Transform PlayerOne;
 public Transform PlayerTwo;
 public Transform Ball;
 
 public bool Ready = false;
 
 private float MinX = -46.0f;
 private float MaxX = 46.0f;
 
 private float MinY = 12.0f;
 private float MaxY = 35.0f;
 
 private float LimitFromPlayerX = 20.0f;
 private float LimitFromPlayery = 20.0f;
 
 private float DistanceToGround = 0.0f;
 private float DistanceFromCenter = 2.0f;
 
 private float Height = 4.0f;
 
 private float Damping = 5f;
 
 private Vector3 EstimatedCameraPosition = Vector3.zero;
 private Vector3 WantedPosition = Vector3.zero;
 
 void Awake()
 {
     Instance = this;
 
 }
 
 public void SetCameraPosition()
 {
     Vector3 PlayerPosistion = PlayerOne.position;
 
     PlayerPosistion.z = -10.10f;
 
     transform.position = PlayerPosistion;
 }
 
 // Update is called once per frame
 void FixedUpdate () {
     if (Ready) {
         Vector3 PlayerPosistion = PlayerOne.position;
 
         PlayerPosistion.z = -10.10f;
 
         Vector3 CameraPosition = transform.position;
 
         CameraPosition.z = -10.10f;
 
         // Get the Distance from the Player to the Ball //
         Vector3 WantedPosition = PlayerTwo.transform.position - -PlayerOne.transform.position;
 
         float WPMag = Mathf.Abs(WantedPosition.magnitude);
 
         WPMag = Mathf.Clamp(WPMag, 20, 40);
 
         Debug.Log(WPMag);
 
         Camera.main.orthographicSize = WPMag;
 
         transform.position = Vector3.Lerp(CameraPosition, WantedPosition, Damping * Time.deltaTime);
     }
 }
 
 void LateUpdate()
 {
     Vector3 Posit = transform.position;
     Posit.x = Mathf.Clamp (Posit.x, MinX, MaxX);
     Posit.y = Mathf.Clamp (Posit.y, MinY, MaxY);
 
     transform.position = Posit;
 }
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

26 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

Related Questions

How can I get exact screen position of 3D game object in front of camera while using cardboard in VR game? 1 Answer

RTS Camera movement wrong after rotation 1 Answer

Camera Follow Player using a path 0 Answers

Getting camera to stick to object 1 Answer

Script to move between camers 1 Answer


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