Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 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 /
avatar image
0
Question by cornell-lindsay · Mar 01, 2015 at 09:58 AM · cameracamera-movementcamera followcamera movement

Make a camera to Follow two players

so in my game i have a local co-op and i want a single camera to follow the players. this camera should be at a specified distance from the players but zoom in or zoom out, smoothly based on the distance between the two players like most old coop games. i have attempted to make it but it does not really work. can anyone suggest how to fix what i have or a whole new way of doing it. any help appreciated.

public class CameraFollow : MonoBehaviour {

 public Vector3 distance;
 public Transform target1;
 public Transform target2;
 public float CamZPosition;
 public float TargetZmiddle;
 public float camDistance;
 public float trial;

 // Use this for initialization
 void Start () 
 {
     
 }
 
 // Update is called once per frame
 void Update () 
 {
     distance = target1.position - target2.position;
     TargetZmiddle = ((target1.position.z * target2.position.z) /2);

     if(distance.x < 0)
         distance.x = distance.x * -1;
     if(distance.z < 0)
         distance.z = distance.z * -1;

     trial = camDistance + distance.x;


     if(distance.z > 20)
     {
         //camDistance = trial;
     }

     if(distance.x > 20)
     {
         camDistance = trial;
     }
     if(distance.x < 19 || distance.z < 19)
     {
         camDistance = 10;
     }


     //Debug.Log (TargetZmiddle);
     transform.position = new Vector3(transform.position.x, transform.position.y, (TargetZmiddle - camDistance));

 }

}

Comment
Add comment · Show 6
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 meat5000 ♦ · Feb 28, 2015 at 09:56 PM 1
Share

Point the camera halfway between the players. (I think you've done this)

$$anonymous$$ight be simple to use 'isVisible' to deter$$anonymous$$e when the camera stops zoo$$anonymous$$g.

Really though, you will want to work out the frustum based on distances which can be worked out on paper with a few triangles.

You're method is probably getting there but a bit of trial and error is going to be the key.

avatar image meat5000 ♦ · Mar 01, 2015 at 01:52 AM 0
Share

distance and camDistance are not related. What are they intended to do?

pos 1 and pos2 are both set from camera position and both feed back in to camera position one after the other.

avatar image cornell-lindsay · Mar 01, 2015 at 02:07 AM 0
Share

Your right CamDistance and distance are not related in any way distance is a the distance between the two players as a vector 3

CamDistance is the value in the z axis that the camera will be 'zoomed' from the halfway point of the players.

Pos is a workaround as I couldn't get math clamp working so once the players are near the camera edge they cannot move any further from the camera view

avatar image meat5000 ♦ · Mar 01, 2015 at 02:37 AM 0
Share

camDistance doesn't seem to get updated.

You could use this to move the camera backwards to extend the view.

avatar image cornell-lindsay · Mar 01, 2015 at 02:50 AM 0
Share

Thats what CamOffset does, CamDistance stays at 10 to make sure the camera stays at least 10 units away from the midpoint as without it when the players got close to each other the camera would zoom in soo much that they would take up the whole screen

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by cornell-lindsay · Mar 01, 2015 at 01:18 AM

for anyone that wants to know i have completed this function the final code is below

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class CameraFollow : MonoBehaviour { public static CameraFollow cFollow;

 public float dampTime = 0.15f;
 private Vector3 velocity = Vector3.zero;
 public Transform target;

 public float MidX;
 public float MidY;
 public float MidZ;

 public Transform target1;
 public Transform target2;
 public Vector3 Midpoint;

 public Vector3 distance;
 public float camDistance;

 public float CamOffset;
 public float bounds;

 void Awake () 
 {
     //checking if there's already a game manager
     if(cFollow == null)
     {
         DontDestroyOnLoad (gameObject);    
         cFollow = this;
     }
     else if(cFollow != this)
     {
         Destroy(gameObject);
     }    
 }
 
 // Use this for initialization
 void Start () 
 {
     camDistance = 10.0f;
     bounds = 12.0f;
 }
 
 // Update is called once per frame
 void Update () 
 {
     distance = target1.position - target2.position;

     if(camDistance >= 19.0f)
        camDistance = 19.0f;
     if (camDistance <= 10.0f)
         camDistance = 10.0f;

     if(distance.x < 0)
         distance.x = distance.x * -1;
     if(distance.z < 0)
         distance.z = distance.z * -1;

     if(target1.position.x < (transform.position.x -bounds))
     {
         Vector3 pos = target1.position;
         pos.x =  transform.position.x -bounds;
         target1.position = pos;
     }
     if(target2.position.x < (transform.position.x -bounds))
     {
         Vector3 pos = target2.position;
         pos.x =  transform.position.x -bounds;
         target2.position = pos;
     }
     if(target1.position.x > (transform.position.x +bounds))
     {
         Vector3 pos = target1.position;
         pos.x =  transform.position.x +bounds;
         target1.position = pos;
     }
     if(target2.position.x > (transform.position.x +bounds))
     {
         Vector3 pos = target2.position;
         pos.x =  transform.position.x +bounds;
         target2.position = pos;
     }

     if(distance.x > 15.0f)
     {
         CamOffset = distance.x * 0.3f;

         if(CamOffset >=8.5f)
             CamOffset = 8.5f;

     }else if(distance.x < 14.0f)
     {
         CamOffset = distance.x * 0.3f;
     }else if( distance.z < 14.0f)
     {
         CamOffset = distance.x * 0.3f;
     }

     MidX = (target2.position.x + target1.position.x) /2; 
     MidY = (target2.position.y + target1.position.y) /2;
     MidZ = (target2.position.z + target1.position.z) /2;

     Midpoint = new Vector3 (MidX, MidY, MidZ);

     if (target1)
     {
         Vector3 point = camera.WorldToViewportPoint(Midpoint);
         Vector3 delta = Midpoint - camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, camDistance + CamOffset)); //(new Vector3(0.5, 0.5, point.z));
         Vector3 destination = transform.position + delta;
         transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
     }
     
 }
Comment
Add comment · Show 1 · 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 cornell-lindsay · May 19, 2021 at 03:20 AM 0
Share

this was the code used pretty sure its exactly the same as previous poster

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class CameraFollow : MonoBehaviour { public static CameraFollow cFollow;

 public float dampTime = 0.15f;
 private Vector3 velocity = Vector3.zero;
 public Transform target;

 public float MidX;
 public float MidY;
 public float MidZ;

 public Transform target1;
 public Transform target2;
 public Vector3 Midpoint;

 public Vector3 distance;
 public float camDistance;

 public float CamOffset;
 public float bounds;
 
 // Use this for initialization
 void Start () 
 {
     camDistance = 10.0f;
     bounds = 12.0f;
 }
 
 // Update is called once per frame
 void Update () 
 {
     distance = target1.position - target2.position;

     if(camDistance >= 19.0f)
        camDistance = 19.0f;
     if (camDistance <= 10.0f)
         camDistance = 10.0f;

     if(distance.x < 0)
         distance.x = distance.x * -1;
     if(distance.z < 0)
         distance.z = distance.z * -1;

     if(target1.position.x < (transform.position.x -bounds))
     {
         Vector3 pos = target1.position;
         pos.x =  transform.position.x -bounds;
         target1.position = pos;
     }
     if(target2.position.x < (transform.position.x -bounds))
     {
         Vector3 pos = target2.position;
         pos.x =  transform.position.x -bounds;
         target2.position = pos;
     }
     if(target1.position.x > (transform.position.x +bounds))
     {
         Vector3 pos = target1.position;
         pos.x =  transform.position.x +bounds;
         target1.position = pos;
     }
     if(target2.position.x > (transform.position.x +bounds))
     {
         Vector3 pos = target2.position;
         pos.x =  transform.position.x +bounds;
         target2.position = pos;
     }

     if(distance.x > 15.0f)
     {
         CamOffset = distance.x * 0.3f;

         if(CamOffset >=8.5f)
             CamOffset = 8.5f;

     }else if(distance.x < 14.0f)
     {
         CamOffset = distance.x * 0.3f;
     }else if( distance.z < 14.0f)
     {
         CamOffset = distance.x * 0.3f;
     }

     MidX = (target2.position.x + target1.position.x) /2; 
     MidY = (target2.position.y + target1.position.y) /2;
     MidZ = (target2.position.z + target1.position.z) /2;

     Midpoint = new Vector3 (MidX, MidY, MidZ);

         Vector3 delta = Midpoint - camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, camDistance + CamOffset));
         Vector3 destination = transform.position + delta;
         transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
     
 }


}

if that doesnt work drop your email and i'll send you the whole unity file and you can reverse engineer it from that

avatar image
0

Answer by byassine193 · May 06, 2021 at 12:21 AM

hellp hi bro i want use this script but is not working

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Help make camera zoom smoother 3 Answers

Smooth camera on moving platform 1 Answer

How to make a camera Follow an Object moving in zigzag path? 1 Answer

camera follow gameobject moving in angular path. 0 Answers

Making camera follow upwards 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