Unity 分屏同机多人游戏教程

在本教程中,我将展示如何在 Unity 中制作分屏 multiplayer

脚步

  • 使用您的关卡打开一个场景(在我的例子中,它将是一个带有一些立方体的简单场景)

  • 创建一个新的 GameObject 并调用它 "Player 1"
  • 创建一个新的立方体并将其移动到 "Player 1" 对象内(删除其 Box Collider 组件)
  • 为眼睛和嘴巴创建更多立方体(也删除它们的 Box Collider 组件)

  • 将主摄像机移动到 "Player 1" 对象内并将其指向立方体

  • 创建 一个新脚本,将其命名为 "RigidbodyPlayerController" 并将以下代码粘贴到其中:

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);
        }
    }
}
  • RigidbodyPlayerController 脚本附加到 "Player 1" (您会注意到它将添加另外 2 个组件,Rigidbody 和 Capsule Collider)
  • 调整胶囊碰撞器直到它与立方体尺寸匹配。

以下是制作 2 人分屏游戏的步骤:

  • 复制 "Player 1" 对象并将其重命名为 "Player 2"。
  • 在 RigidbodyPlayerController 中将 Player Controls 更改为 "Arrows"。

  • 将 "Player 1" 相机的视口矩形值更改为 X: 0 Y: 0.5 W: 1 H: 0.5

  • 将"Player 2"相机的视口矩形值更改为 X: 0 Y: 0 W: 1 H: 0.5

或者,您可以通过设置以下值来设置垂直分屏:

X: 0 Y: 0 W: 0.5 H: 1 对于摄像机 1

X: 0.5 Y: 0 W: 0.5 H: 1 用于摄像机 2

来源
📁Split-Screen.unitypackage27.74 KB
推荐文章
在 Unity 中创建简单的草着色器
在 Unity 中创建获胜者屏幕 UI
在 Unity 中创建暂停菜单
在 Unity 中创建飞行模拟器
在 Unity 中创建 VHS 磁带滤镜效果
为您的 Unity 项目选择正确的 Sword 模型
如何将僵尸添加到 Unity 游戏中