# 2d platform, 2 player, 1 camera

**URL:** https://discussions.unity.com/t/2d-platform-2-player-1-camera/400261
**Category:** Camera
**Tags:** Scripting
**Created:** [October 27, 2009, 12:55pm UTC](https://discussions.unity.com/t/2d-platform-2-player-1-camera/400261 "2009-10-27T12:55:54Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![anon\_57047471](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@anon\_57047471](https://discussions.unity.com/u/anon_57047471)
#### Post date: [October 27, 2009, 12:55pm UTC](https://discussions.unity.com/t/2d-platform-2-player-1-camera/400261/1 "2009-10-27T12:55:54Z")

</div>

Hi, i have a question over here.  
I am doing a 2d platform, with player 1 at the left side of the screen, and player 2 at the right side of the screen facing each other. The camera is showing both players. There is a maximum distance between player 1 and player 2 for the camera to show.  
When player 1 moves backwards and reach the maximum distance, he cannot move backwards.

I uses Mathf.Clamp for both player1 and 2. But it seems that when player1 moves backwards, he is dragging player2 along too.

How do I do that?  
Thanks.

---

<div class="post-metadata">

### Author: ![Narlix](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/narlix/32/254464_2.png) [@Narlix](https://discussions.unity.com/u/Narlix)
#### Post date: [October 27, 2009, 2:03pm UTC](https://discussions.unity.com/t/2d-platform-2-player-1-camera/400261/2 "2009-10-27T14:03:06Z")

</div>

use a if command and the Vector3.Distance.

---

<div class="post-metadata">

### Author: ![anon\_57047471](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@anon\_57047471](https://discussions.unity.com/u/anon_57047471)
#### Post date: [October 27, 2009, 2:21pm UTC](https://discussions.unity.com/t/2d-platform-2-player-1-camera/400261/3 "2009-10-27T14:21:37Z")

</div>

-addon:  
When the player1 moves backwards, and reach the maximum distance between both players, there was like an invisible wall, the player can’t move anymore backwards. how?

---

<div class="post-metadata">

### Author: ![Narlix](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/narlix/32/254464_2.png) [@Narlix](https://discussions.unity.com/u/Narlix)
#### Post date: [October 27, 2009, 2:41pm UTC](https://discussions.unity.com/t/2d-platform-2-player-1-camera/400261/4 "2009-10-27T14:41:16Z")

</div>

This is a simple version of it.  
then the distance also should relay on the camera distance, but i trust that you can figure out that part. 😛

PlayerScript.js

```auto
var speed = 3.0;
//var rotateSpeed = 3.0;

function Update ()
{
    var controller : CharacterController = GetComponent(CharacterController);

    // Rotate around y - axis
    //transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
    
    // Move forward / backward
    var forward = transform.TransformDirection(Vector3.forward);
    var curSped = speed * Input.GetAxis ("Horizontal");
    controller.SimpleMove(forward * curSped);
}

@script RequireComponent(CharacterController)

```

Arena.js

```auto
var Player1: GameObject;
var Player2: GameObject;

var MaxDistance: float;

function Start() {
  Player1 = GameObject.Find("player1");
  Player2 = GameObject.Find("player2");	
}

function Update () {
  var distance = Vector3.Distance(Player1.transform.position, Player2.transform.position);
  if (distance > MaxDistance) {
    Player1.transform.position.z = Player2.transform.position.z + -MaxDistance+0.001;	
  }	
}

```

---

<div class="post-metadata">

### Author: ![Narlix](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/narlix/32/254464_2.png) [@Narlix](https://discussions.unity.com/u/Narlix)
#### Post date: [October 27, 2009, 2:45pm UTC](https://discussions.unity.com/t/2d-platform-2-player-1-camera/400261/5 "2009-10-27T14:45:25Z")

</div>

But i guess you and thinking of a tekken like game, so what i would do is, have the camera’s distance to go out to a point and have a box colliders on each end, with a width of lets say 15, so and when the players come closer to each other the camera simply zooms in…

---

<div class="post-metadata">

### Author: ![anon\_57047471](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@anon\_57047471](https://discussions.unity.com/u/anon_57047471)
#### Post date: [October 28, 2009, 1:21am UTC](https://discussions.unity.com/t/2d-platform-2-player-1-camera/400261/6 "2009-10-28T01:21:10Z")

</div>

k, thanks. i will go and try it now.
