Easy Split-Screen Multiplayer in Unity 3D
In this tutorial, I will be showing how to make a split-screen multiplayer in Unity 3D.
Steps
- Open a Scene with your level (in my case it’ll be a simple Scene with some Cubes)
![](https://sharpcoderblog.com/public/images/42b/42bc77cef1708c8ba08ea0a28af33710.png)
- Create a new GameObject and call it “Player 1”
- Create a new Cube and move it inside “Player 1” object (Remove its Box Collider component)
- Create few more Cubes for the eyes and mouth (Remove their Box Collider components as well)
![](https://sharpcoderblog.com/public/images/52f/52ffb8cd5acd01c42ee90f0bcdae645f.png)
- Move the Main Camera inside “Player 1” object and point it at a Cube
![](https://sharpcoderblog.com/public/images/231/23186c1b8724e7ccda77ac72c3413f5f.png)
![](https://sharpcoderblog.com/public/images/ebe/ebe8b372e40765a65f2c509ed13851e8.png)
- Create a new Script, name it “RigidbodyPlayerController” and paste the code below inside it:
RigidbodyPlayerController.cs
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(Rigidbody))]
[RequireComponent(typeof(CapsuleCollider))]
public class RigidbodyPlayerController : MonoBehaviour
{
public enum PlayerControls { WASD, Arrows }
public PlayerControls playerControls = PlayerControls.WASD;
public float movementSpeed = 3f;
public float rotationSpeed = 5f;
Rigidbody r;
float gravity = 10.0f;
void Awake()
{
r = GetComponent<Rigidbody>();
r.freezeRotation = true;
r.useGravity = false;
}
// Update is called once per frame
void FixedUpdate()
{
// Move Front/Back
Vector3 targetVelocity = Vector3.zero;
if ((playerControls == PlayerControls.WASD && Input.GetKey(KeyCode.W)) || (playerControls == PlayerControls.Arrows && Input.GetKey(KeyCode.UpArrow)))
{
targetVelocity.z = 1;
}
else if ((playerControls == PlayerControls.WASD && Input.GetKey(KeyCode.S)) || (playerControls == PlayerControls.Arrows && Input.GetKey(KeyCode.DownArrow)))
{
targetVelocity.z = -1;
}
targetVelocity = transform.TransformDirection(targetVelocity);
targetVelocity *= movementSpeed;
// Apply a force that attempts to reach our target velocity
Vector3 velocity = r.velocity;
Vector3 velocityChange = (targetVelocity - velocity);
float maxVelocityChange = 10.0f;
velocityChange.x = Mathf.Clamp(velocityChange.x, -maxVelocityChange, maxVelocityChange);
velocityChange.z = Mathf.Clamp(velocityChange.z, -maxVelocityChange, maxVelocityChange);
velocityChange.y = 0;
r.AddForce(velocityChange, ForceMode.VelocityChange);
// We apply gravity manually for more tuning control
r.AddForce(new Vector3(0, -gravity * r.mass, 0));
// Rotate Left/Right
if ((playerControls == PlayerControls.WASD && Input.GetKey(KeyCode.A)) || (playerControls == PlayerControls.Arrows && Input.GetKey(KeyCode.LeftArrow)))
{
transform.Rotate(new Vector3(0, -14, 0) * Time.deltaTime * rotationSpeed, Space.Self);
}
else if ((playerControls == PlayerControls.WASD && Input.GetKey(KeyCode.D)) || (playerControls == PlayerControls.Arrows && Input.GetKey(KeyCode.RightArrow)))
{
transform.Rotate(new Vector3(0, 14, 0) * Time.deltaTime * rotationSpeed, Space.Self);
}
}
}
- Attach RigidbodyPlayerController script to “Player 1” (You’ll notice it will add 2 more components, Rigidbody and Capsule Collider)
- Tweak Capsule Collider till it matches the Cube dimensions.
Below are steps to make 2 player split-screen:
- Duplicate “Player 1” object and rename it to “Player 2”.
- In RigidbodyPlayerController change Player Controls to “Arrows”.
![](https://sharpcoderblog.com/public/images/0e6/0e66d27783afb875cd840f3e3cce07a8.png)
- Change Viewport Rect values of “Player 1” Camera to X: 0 Y: 0.5 W: 1 H: 0.5
![](https://sharpcoderblog.com/public/images/aaa/aaae24770fab4cdb1510f98898f49dae.png)
- Change Viewport Rect values of “Player 2” Camera to X: 0 Y: 0 W: 1 H: 0.5
![](https://sharpcoderblog.com/public/images/d81/d8128e14ab25f5cd39ea5dabeaa84d62.png)
![](https://sharpcoderblog.com/public/images/dea/deace92adfbe7bdbed0fd25a74f8fcbe.png)
Alternatively you can set up a vertical split-screen by setting the values below:
X: 0 Y: 0 W: 0.5 H: 1 for Camera 1
X: 0.5 Y: 0 W: 0.5 H: 1 for Camera 2
![](https://sharpcoderblog.com/public/images/d1b/d1be38db3b9a694143023555dc279e26.png)