- Home /
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.
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:
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.
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...?
Hey Lockedown5000, sounds like you aren't calling the method. Try calling it every frame in Update() with the two character's transforms.
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
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.
Putting a camera between two (or any number of) objects doesn't guarantee that you will see either of them.
Your answer
Follow this Question
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