Tutorial multiplayer pe același computer cu ecran împărțit pentru Unity

În acest tutorial, voi arăta cum să faci un multiplayer cu ecran divizat în Unity.

Pași

  • Deschide o scenă cu nivelul tău (în cazul meu va fi o scenă simplă cu câteva cuburi)

  • Creați un nou GameObject și apelați-l "Player 1"
  • Creați un cub nou și mutați-l în interiorul obiectului "Player 1" (Eliminați componenta lui Box Collider)
  • Creați încă câteva cuburi pentru ochi și gură (înlăturați și componentele Box Collider)

  • Mutați camera principală în interiorul obiectului "Player 1" și îndreptați-o către un cub

  • Creați un nou Script, numiți-l "RigidbodyPlayerController" și inserați codul de mai jos în el:

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);
        }
    }
}
  • Atașați scriptul RigidbodyPlayerController la "Player 1" (veți observa că va adăuga încă 2 componente, Rigidbody și Capsule Collider)
  • Ajustați Capsule Collider până când se potrivește cu dimensiunile Cubului.

Mai jos sunt pașii pentru a crea un ecran divizat pentru 2 jucători:

  • Duplicați obiectul "Player 1" și redenumiți-l în "Player 2".
  • În RigidbodyPlayerController, schimbați Controlul jucătorului în "Arrows".

  • Schimbați valorile Viewport Rect ale "Player 1" Camera la X: 0 Y: 0,5 W: 1 H: 0,5

  • Schimbați valorile Viewport Rect ale "Player 2" Camera la X: 0 Y: 0 W: 1 H: 0,5

Alternativ, puteți configura un ecran divizat vertical setând valorile de mai jos:

X: 0 Y: 0 W: 0,5 H: 1 pentru Camera 1

X: 0,5 Y: 0 W: 0,5 H: 1 pentru Camera 2

Sursă
📁Split-Screen.unitypackage27.74 KB
Articole sugerate
Prezentare generală de tip Minimap Tutorial pentru Unity
Crearea unui ecran de încărcare în Unity
Tutorial cu efect de strălucire obiect pentru Unity
Tutorial meniul principal pentru Unity
Tutorial de post-procesare cu efect de imagine pentru vedere nocturnă pentru Unity
Cum să creezi un joc de groază în Unity
Crearea unui umbritor de iarbă simplu în Unity