c# - 如何为使用同一键盘的 2 名玩家分配不同的控制方案?
问题描述
我正在创建一个本地多人游戏,其中 2 名玩家使用相同的键盘输入控件。我为玩家创建了 2 个不同的控制方案,但我正在努力寻找一种方法来为每个玩家角色分配不同的方案以及如何检查哪个玩家正在使用哪个方案等
解决方案
转到Unity GetAxis,您可以从
编辑 > 项目设置 > 输入管理器
如果您使用名称“ 2ndPlayerHorizontal ”、“ 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);
}
}
推荐阅读
- time - 谐波级数的大哦时间复杂度
- python - 有没有办法让 cm 在 Reportlab 中工作?我已经尝试导入这些单位,但他们返回不准确的暗淡
- python - 如何从python中的文本文件中读取单词/字符串?
- go - gpg 在 goreleaser 中失败
- html - 水印将内容对齐到最左端
- asp.net - 我无法在我的 ASP.net 项目 (Visual Studio 2019) 上提取接口类
- unity3d - 角色控制器不再掉落
- javascript - 如何验证路径参数?
- r - Shiny - 如何在 PickerInput 中选择整个组?
- c++ - 如何在 C++ 中为 multimap 实现以下函数 getValue(m)