Split-Screen Same-PC Multiplayer Tutorial for Unity

In this tutorial, I will be showing how to make a split-screen multiplayer in Unity.

Steps

  • Open a Scene with your level (in my case it'll be a simple Scene with some Cubes)

  • Create a new GameObject and call it "Player 1"
  • Create a new Cube and move it inside the "Player 1" object (Remove its Box Collider component)
  • Create a few more Cubes for the eyes and mouth (Remove their Box Collider components as well)

  • Move the Main Camera inside the "Player 1" object and point it at a Cube

  • 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 the 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 the steps to make a 2-player split-screen:

  • Duplicate the "Player 1" object and rename it to "Player 2".
  • In RigidbodyPlayerController change Player Controls to "Arrows".

  • Change Viewport Rect values of "Player 1" Camera to X: 0 Y: 0.5 W: 1 H: 0.5

  • Change Viewport Rect values of "Player 2" Camera to X: 0 Y: 0 W: 1 H: 0.5

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

Source
📁Split-Screen.unitypackage27.74 KB
Suggested Articles
Main Menu Tutorial for Unity
Object Glow Effect Tutorial for Unity
Overview-type Minimap Tutorial for Unity
Creating a Simple Grass Shader in Unity
Creating a Pause Menu in Unity
Choosing the Right Sword Models for Your Unity Project
Power of Placeholder Textures for Prototyping in Unity