首页 > 解决方案 > 如何为使用同一键盘的 2 名玩家分配不同的控制方案?

问题描述

我正在创建一个本地多人游戏,其中 2 名玩家使用相同的键盘输入控件。我为玩家创建了 2 个不同的控制方案,但我正在努力寻找一种方法来为每个玩家角色分配不同的方案以及如何检查哪个玩家正在使用哪个方案等

标签: c#unity3dinputcontrolsmultiplayer

解决方案


转到Unity GetAxis,您可以从

编辑 > 项目设置 > 输入管理器

如果您使用名称“ 2ndPlayerHorizo​​ntal ”、“ 2ndPlayerVertical ”制作另外2个轴并在播放器上放置标签(“ FirstPlayer ”、“ SecondPlayer ”)

您可以使用以下代码:

using UnityEngine;
using System.Collections;

public class PlayersMovement: MonoBehaviour
{
    public float speed = 10.0f;
    public float rotationSpeed = 100.0f;

    float translation;
    float rotation;

    void Update()
    {
        if(this.CompareTag("FirstPlayer")
        {
            //1st player
            translation = Input.GetAxis("Vertical") * speed;
            float rotation = Input.GetAxis("Horizontal") * rotationSpeed;
        }

        if(this.CompareTag("SecondPlayer")
        {
            //2nd player
            translation = Input.GetAxis("Vertical") * speed;
            rotation = Input.GetAxis("Horizontal") * rotationSpeed;
        }

        translation *= Time.deltaTime;
        rotation *= Time.deltaTime;
        transform.Translate(0, 0, translation);
        transform.Rotate(0, rotation, 0);
    }
}

推荐阅读