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
0
Question by Bradley_FDA · Feb 15, 2016 at 11:22 AM · cameramovement scriptplayer movementtarget

Moving Camera With 2 Players

I have a game set-up with 2 players, both with separate movement scripts of course. My main concern is that the camera must move with the player(s). I can parent the camera to just one player but then the other one can get left behind.

What I'm trying to do is get the camera to follow a target, which follows the midpoint of both players. I know how to find the midpoint of the players (http://forum.unity3d.com/threads/midpoint-between-two-objects.38512/). I also know how to move a target to a set destination (http://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html) but I need help combining the two.

Please help if you can, thanks in advance.

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

3 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by TreyH · Feb 15, 2016 at 01:24 PM

We'll keep it simple and assume you have a camera with fixed rotation (it will be simple to further this functionality for a rotating camera (or any number of transforms) given a little work on paper):

 // Follow Two Transforms with a Fixed-Orientation Camera
 public void FixedCameraFollowSmooth(Camera cam, Transform t1, Transform t2)
 {
     // How many units should we keep from the players
     float zoomFactor = 1.5f;
     float followTimeDelta = 0.8f;
 
     // Midpoint we're after
     Vector3 midpoint = (t1.position + t2.position) / 2f;
 
     // Distance between objects
     float distance = (t1.position - t2.position).magnitude;
 
     // Move camera a certain distance
     Vector3 cameraDestination = midpoint - cam.transform.forward * distance * zoomFactor;
 
     // Adjust ortho size if we're using one of those
     if (cam.orthographic)
     {
         // The camera's forward vector is irrelevant, only this size will matter
         cam.orthographicSize = distance;
     }

     // You specified to use MoveTowards instead of Slerp
     cam.transform.position = Vector3.Slerp(cam.transform.position, cameraDestination, followTimeDelta);
         
     // Snap when close enough to prevent annoying slerp behavior
     if ((cameraDestination - cam.transform.position).magnitude <= 0.05f)
         cam.transform.position = cameraDestination;
 }

Result:

http://i.imgur.com/G6KjNMz.gif

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 kryzodoze · Apr 19, 2019 at 09:45 PM 0
Share

This worked perfectly for me, thanks! Fyi to others, the "followTimeDelta" is how far between the camera's current position and the final position you want to travel. So, if you're calling this in Update() for instance, you may want to decrease that to something like 0.1f or lower if you don't want it to jump as quickly.

avatar image Lockes_TheThief · Apr 21, 2019 at 09:42 PM 0
Share

Hey, where exactly does this code go? I made two characters with two scripts and I wanted to use this. I put it on the camera but it doesn't seem to do anything...?

avatar image kryzodoze Lockes_TheThief · Apr 22, 2019 at 02:20 AM 0
Share

Hey Lockedown5000, sounds like you aren't calling the method. Try calling it every frame in Update() with the two character's transforms.

avatar image
-1

Answer by MZShahmy · Aug 28, 2017 at 03:54 AM

hey can i see your scripts please?

i want it please?

i want to make a two player game too...but i dont know how to make the scripts for the second player

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
avatar image
0

Answer by Jaws959 · Feb 15, 2016 at 02:19 PM

Make a script and attach it to the camera. Make the script change the position of the camera by averaging the vector 3 positions of all the active player gameObjects. You might have to average all x values and y values (assuming 2D) unless there is a method somewhere which does it for you.

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 TreyH · Feb 15, 2016 at 03:40 PM 1
Share

Putting a camera between two (or any number of) objects doesn't guarantee that you will see either of them.

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

49 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

Related Questions

How to achieve 2.5D movement along curve? 1 Answer

player following touch position 0 Answers

Solve Issue of Camera looking through walls when in them 0 Answers

Camera and Target JS to CS converting 2 Answers

Physics based movement (Moving to fast) 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