Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
4 captures
13 Jun 22 - 14 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 /
This question was closed Oct 05, 2021 at 08:15 PM by meganbrackett for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by meganbrackett · Sep 08, 2021 at 03:17 PM · camerascripting beginneroffset

Position camera between two players

I'm trying to figure out how to calculate the offset position of the camera to follow both players based on the midpoint and determine the new camera position. I have most of it figured out, can anyone take a look and point me in the right direction?

Here's what I have so far:

{ public GameObject player1; public GameObject player2;

 private Vector3 offset;
 private Vector3 midPosition;

 // Start is called before the first frame update
 void Start()
 {
     midPosition = (player1.transform.position + player2.transform.position) / 2.0f;

     // Need to calculate offset based on midpoint
     offset = transform.position - player1.transform.position;
 }

 // Update is called once per frame
 void Update()
 {
     midPosition = (player1.transform.position + player2.transform.position) / 2.0f;

     // Need to calculate new camera position based on midpoint
     transform.position = offset + player1.transform.position;
 }

}

Comment
Add comment · Show 1
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 Cherie · Sep 08, 2021 at 04:41 PM 1
Share

It looks like it's all there - the last line just needs to use the calculated midPosition: transform.position = offset + midPosition;

1 Reply

  • Sort: 
avatar image
0

Answer by GameBrews · Sep 08, 2021 at 06:09 PM

As suggested by @Cherie , it does seem to be in order except the last line. You set the new position without using the calculated midPosition.

change:

 transform.position = offset + player1.transform.position;

to:

 transform.position = offset + midPosition

And about the offset: It is calculated once inside Start(), before you reposition the camera between the players, so the initial offset between where you placed player1 and the camera in the scene editor will be used when the scene is running.

You can address this by calculating the midpoint once inside Start(), as you already have, set the camera position to it and only than calculate the offset.,As suggested by @Cherie , it does seem to be in order except the last line. You set the new position without using the calculated midPosition.

change:

 transform.position = offset + player1.transform.position;

to:

 transform.position = offset + midPosition

And about the offset: It is calculated once inside Start(), before you reposition the camera between the players, so the initial offset between where you placed player1 and the camera in the scene editor will be used when the scene is running.

You can address this by calculating the midpoint once inside Start(), as you already have, set the camera position to it and only than calculate the offset.

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

Follow this Question

Answers Answers and Comments

195 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

Related Questions

Editor Gizmo logic question? How do the gizmos move relative to camera 1 Answer

How to make the camera follow and face a rolling object? 2 Answers

Camera Rotation and Zoom From select object 1 Answer

Rotatearaound a moving object 1 Answer

Can I connect two camera's to each other? 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