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 chiggafoo · Jun 25, 2012 at 07:01 PM · cameraplayertwoone

2 Players 1 Camera

How would i go about on creating camera that followed 2 players but it would limit to lets say when one player is at the left border of the camera and the other player cannot move anymore unless the other player would move right.

Here my crack at the script, but this did not work.

I tried to check if the players were moving left or right and when the distance were to far apart the camera object couldn't move anymore. This didn't work as plan because the camera still moved, not sure why

Could someone lead me the way to a better way to achieve this? Or examples?

 using UnityEngine;
 
 public class TwoPlayerCam : MonoBehaviour 
 {
     public GameObject Player1;
     public GameObject Player2;
 
     public Camera CameraMove;
     public float MaxDistance;
 
     public bool camCanMoveLeft = true;
     public bool camCanMoveRight = true;
 
     void Start(){
 
         Player1 = GameObject.FindGameObjectWithTag("player1");
         
         Player2 = GameObject.FindGameObjectWithTag("player2"); 
         MaxDistance = camera.orthographicSize;
     }
 
     void Update()
     {
         var rotation1 = Player1.transform.position;
         var rotation2 = Player2.transform.position;
         var P1Comp = Player1.GetComponent<CharMovement>();
         var P2Comp = Player2.GetComponent<CharMovementPlayerTwo>();
 
         var distance = Vector3.Distance(rotation1,rotation2);
         if (distance > MaxDistance && camCanMove==true){
             print("far");
             CameraMove.transform.position += new Vector3(.85f,0f,0f);
             camCanMove=false;
         } 
         else if (distance > MaxDistance && camCanMoveRight==true){
             print("far");
             CameraMove.transform.position += new Vector3(-.85f,0f,0f);
             camCanMoveRight=false;
         } 
         else if(MaxDistance > distance){
             camCanMoveRight=true;
             camCanMove=true;
         }
 
         if(P1Comp.state == CharMovement.WalkingState.WalkLeft || P2Comp.state == CharMovementPlayerTwo.WalkingState.WalkLeft && camCanMoveLeft)
             CameraMove.transform.position += new Vector3(-.85f,0f,0f);
 
         else if(P1Comp.state == CharMovement.WalkingState.WalkRight || P2Comp.state == CharMovementPlayerTwo.WalkingState.WalkRight && camCanMoveRight)
             CameraMove.transform.position += new Vector3(.85f,0f,0f);
     }
 }
Comment
Add comment · Show 3
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 whydoidoit · Jun 25, 2012 at 07:13 PM 0
Share

So you want the camera at a fixed distance? Do you want to camera to rotate or remain in the same relative position?

avatar image Loius · Jun 25, 2012 at 07:20 PM 0
Share

Insert immature giggle here.

avatar image chiggafoo · Jun 26, 2012 at 03:02 AM 0
Share

The script here is very rough.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Loius · Jun 25, 2012 at 07:16 PM

In that script you're not setting camCanMoveLeft=false anywhere, that might be the problem.

Why not just directly set the camera position and prevent player movement if they're too far from each other?

camera.position = (player1.position + player2.position) / 2.0; // then also set the camera's distance along the view normal so it's not directly on the game plane

if ( distance > maxDistance ) player1.position = player2.position + (player1.position-player2.position).normalized * maxDistance;

Comment
Add comment · Show 3 · 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 chiggafoo · Jun 25, 2012 at 10:14 PM 0
Share

It works, but I need to set my camera.position.z to -10, how could I say override it to make the z position to -10 all the time?

avatar image Loius · Jun 26, 2012 at 04:01 AM 0
Share

Simplest way would be to store an 'offset' vector and use that (if p1 and p2 are guaranteed to be on a plane):

var camOffset : Vector3 = Vector3(0, 0, -10);

... camera.position = camOffset + ... ...

You could also just set position.z directly in Update or LateUpdate (i believe Late may work better in this situation, but i dunno)

avatar image chiggafoo · Jun 26, 2012 at 05:18 AM 0
Share

Thank you very much :) so much learning to do!

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

6 People are following this question.

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

Related Questions

Player rotates with camera(face same direction as the camera) 1 Answer

MiniMap with player and object icons 1 Answer

Camera problem 2 Answers

how can I point a ball rolling, given a force to a rigid body, in line to the camera orientation? Sorry i am a beginner 0 Answers

How to make a camera that moves with the player? 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