Tutorial multiplayer sullo stesso PC a schermo diviso per Unity

In questo tutorial mostrerò come creare un multigiocatore a schermo diviso in Unity.

Passi

  • Apri una Scena con il tuo livello (nel mio caso sarà una semplice Scena con alcuni Cubi)

  • Crea un nuovo GameObject e chiamalo "Player 1"
  • Crea un nuovo cubo e spostalo all'interno dell'oggetto "Player 1" (rimuovi il suo componente Box Collider)
  • Crea qualche altro cubo per gli occhi e la bocca (rimuovi anche i componenti Box Collider)

  • Sposta la telecamera principale all'interno dell'oggetto "Player 1" e puntala verso un cubo

  • Crea un nuovo Script, chiamalo "RigidbodyPlayerController" e incolla al suo interno il codice qui sotto:

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);
        }
    }
}
  • Allega lo script RigidbodyPlayerController a "Player 1" (noterai che aggiungerà altri 2 componenti, Rigidbody e Capsule Collider)
  • Modifica Capsule Collider finché non corrisponde alle dimensioni del cubo.

Di seguito sono riportati i passaggi per creare uno schermo diviso per 2 giocatori:

  • Duplica l'oggetto "Player 1" e rinominalo in "Player 2".
  • In RigidbodyPlayerController cambia i controlli del lettore in "Arrows".

  • Modifica i valori Viewport Rect della fotocamera "Player 1" in X: 0 Y: 0,5 W: 1 H: 0,5

  • Modifica i valori Viewport Rect di "Player 2" Camera in X: 0 Y: 0 W: 1 H: 0,5

In alternativa, puoi impostare uno schermo diviso verticale impostando i valori seguenti:

X: 0 Y: 0 W: 0,5 H: 1 per fotocamera 1

X: 0,5 Y: 0 W: 0,5 H: 1 per fotocamera 2

Fonte
📁Split-Screen.unitypackage27.74 KB
Articoli suggeriti
Tutorial sulla minimappa di tipo panoramica per Unity
Tutorial del menu principale per Unity
Creazione di un semplice grass shader in Unity
Come realizzare grafica retrò simile a PS1 in Unity
Come dipingere con il sistema particellare in Unity
Crea un effetto schiuma per idropulitrice in Unity
Tutorial sulla post-elaborazione degli effetti immagine per la visione notturna per Unity