Unity双玩家控制实现:WASD与方向键分别操控角色
为两名玩家设置不同控制方案(WASD/方向键)的实现方法
需求说明
我正在制作一款类似《任天堂明星大乱斗》的游戏,作为编程新手,希望学习如何为两名玩家设置不同的控制方案——一名使用WASD键,一名使用方向键。以下是我的PlayerMovement脚本代码:
using UnityEngine; public class PlayerMovement : MonoBehaviour { [SerializeField] private float gravity; [SerializeField] private float speed; [SerializeField] private LayerMask groundlayer; [SerializeField] private LayerMask platformlayer; private Rigidbody2D body; // 给Unity创建Rigidbody2D的引用 private Animator anim; private BoxCollider2D boxcollider; private void Awake() // 脚本加载时调用Awake { // 从物体上获取Animator和Rigidbody2D的引用 anim = GetComponent<Animator>(); body = GetComponent<Rigidbody2D>(); boxcollider = GetComponent<BoxCollider2D>(); // 检查玩家物体,GetComponent方法会查找该物体上的RB2D组件,并存储到body变量中 } private void Update()// 每帧调用一次 { float horizontalInput = Input.GetAxis("Horizontal"); body.velocity = new Vector2(horizontalInput * speed, body.velocity.y);// 控制角色左右移动 // 左右移动时翻转角色 if (horizontalInput > 0.01f) transform.localScale = Vector3.one; else if (horizontalInput < -0.01f) transform.localScale = new Vector3(-1, 1, 1); if (Input.GetKey(KeyCode.Space) && isGrounded()) Jump(); } private void Jump() { // 跳跃逻辑,这里假设你会添加向上的速度 body.velocity = new Vector2(body.velocity.x, Mathf.Sqrt(gravity * -2 * speed)); } private bool isGrounded() { // 检测是否在地面上 RaycastHit2D hit = Physics2D.BoxCast(boxcollider.bounds.center, boxcollider.bounds.size, 0f, Vector2.down, 0.1f, groundlayer); return hit.collider != null; } }
实现步骤
1. 配置Unity输入管理器
打开Unity编辑器,依次点击 Edit > Project Settings > Input Manager,在输入管理器中添加并配置玩家专属的输入轴:
- 复制默认的
Horizontal轴,重命名为Horizontal_P1,将正方向按键设为D,负方向按键设为A; - 复制默认的
Vertical轴,重命名为Vertical_P1,将正方向按键设为W,负方向按键设为S; - 再复制一份
Horizontal轴,重命名为Horizontal_P2,将正方向按键设为RightArrow,负方向按键设为LeftArrow; - 复制一份
Vertical轴,重命名为Vertical_P2,将正方向按键设为UpArrow,负方向按键设为DownArrow; - 添加跳跃轴:创建新的输入轴
Jump_P1,绑定按键为Space;创建Jump_P2,绑定按键为Enter(或你想要的其他按键)。
2. 修改PlayerMovement脚本
在脚本中添加玩家ID标识,根据不同ID读取对应输入:
using UnityEngine; public class PlayerMovement : MonoBehaviour { [SerializeField] private float gravity; [SerializeField] private float speed; [SerializeField] private LayerMask groundlayer; [SerializeField] private LayerMask platformlayer; [SerializeField] private int playerId; // 新增:玩家ID,在Inspector中设置1或2 private Rigidbody2D body; private Animator anim; private BoxCollider2D boxcollider; private void Awake() { anim = GetComponent<Animator>(); body = GetComponent<Rigidbody2D>(); boxcollider = GetComponent<BoxCollider2D>(); } private void Update() { float horizontalInput = 0; KeyCode jumpKey = KeyCode.Space; // 根据玩家ID获取对应输入 if (playerId == 1) { horizontalInput = Input.GetAxis("Horizontal_P1"); jumpKey = KeyCode.Space; } else if (playerId == 2) { horizontalInput = Input.GetAxis("Horizontal_P2"); jumpKey = KeyCode.Enter; } // 左右移动逻辑 body.velocity = new Vector2(horizontalInput * speed, body.velocity.y); // 翻转角色 if (horizontalInput > 0.01f) transform.localScale = Vector3.one; else if (horizontalInput < -0.01f) transform.localScale = new Vector3(-1, 1, 1); // 跳跃逻辑 if (Input.GetKey(jumpKey) && isGrounded()) Jump(); } private void Jump() { body.velocity = new Vector2(body.velocity.x, Mathf.Sqrt(gravity * -2 * speed)); } private bool isGrounded() { RaycastHit2D hit = Physics2D.BoxCast(boxcollider.bounds.center, boxcollider.bounds.size, 0f, Vector2.down, 0.1f, groundlayer); return hit.collider != null; } }
3. 配置玩家对象
- 在场景中创建两个玩家对象,分别挂载修改后的
PlayerMovement脚本; - 选中玩家1对象,在Inspector面板中把
playerId设为1; - 选中玩家2对象,把
playerId设为2; - 确保两个玩家的地面检测层(groundlayer)配置正确。
内容的提问来源于stack exchange,提问作者speedyraj
相关产品推荐
相关产品推荐

