【问题标题】:How do I give my player object each a playerID attribute and make them move based on different inputs?如何为每个玩家对象赋予一个 playerID 属性并让它们根据不同的输入移动?
【发布时间】:2016-05-31 19:01:46
【问题描述】:
using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{


    public float speed = 80.0f; // Code for how fast the ball can move. Also it will be public so we can change it inside of Unity itself. 
    private Rigidbody rb; // Variable that applies itself to Ball 


    void Start()
    {
      rb = GetComponent<Rigidbody> ();
    }

    // Code That enables our Player to move around. 
    void Movement()
    {

        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");
        Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);
        rb.AddForce (movement * speed);
    }

     // Update is called once per frame
    void Update ()
    {
      Movement ();
    }

【问题讨论】:

  • 我想将代码实现到 Start 方法中。
  • 如果你解释你想要做什么并且解释得很好,也许有人可以理解你的问题并帮助你。什么输入?
  • 输入将是使其移动的键盘键。我有两个玩家对象,但我每个玩家都有不同的键来让它移动
  • 好的。现在说得通了。还有许多其他方法可以在没有 id 或创建另一个类的情况下做到这一点。用播放器一的代码和播放器 2 的代码更新您的问题,我将尝试提供一种方法来做到这一点。
  • 我已经编辑好了

标签: c# unity3d


【解决方案1】:

Input.GetAxis ("Horizontal");Input.GetAxis ("Vertical"); 检测 aswd 和上、下、左、右箭头。您必须弄乱 Unity 输入设置才能更改这些设置,因此如果您使用 Input.GetAxis ("Horizontal");Input.GetAxis ("Vertical"); ,除非您更改 Unity 中定义的键,否则 Unity 将移动两个玩家。解决方案是使用Input.GetKey() 并提供密钥名称。创建两个玩家并将刚体附加到他们身上。然后,您可以在编辑器中将播放器一拖到 Player1RB,将播放器 2 拖到 Player2RB。

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
    public float speed = 80.0f; // Code for how fast the ball can move. Also it will be public so we can change it inside of Unity itself. 
    public Rigidbody player1RB; //Player 1 Rigidbody
    public Rigidbody player2RB; //Player 2 Rigidbody


    //Player 1 Code with aswd keys
    void Player1Movement()
    {
        if (Input.GetKey(KeyCode.A))
        {
            player1RB.AddForce(Vector3.left * speed);

        }

        if (Input.GetKey(KeyCode.D))
        {
            player1RB.AddForce(Vector3.right * speed);

        }

        if (Input.GetKey(KeyCode.W))
        {
            player1RB.AddForce(Vector3.forward * speed);

        }

        if (Input.GetKey(KeyCode.S))
        {
            player1RB.AddForce(Vector3.back * speed);

        }
    }

    //Player 2 Code with arrow keys
    void Player2Movement()
    {
        if (Input.GetKey(KeyCode.LeftArrow))
        {
            player2RB.AddForce(Vector3.left * speed);

        }

        if (Input.GetKey(KeyCode.RightArrow))
        {
            player2RB.AddForce(Vector3.right * speed);

        }

        if (Input.GetKey(KeyCode.UpArrow))
        {
            player2RB.AddForce(Vector3.forward * speed);

        }

        if (Input.GetKey(KeyCode.DownArrow))
        {
            player2RB.AddForce(Vector3.back * speed);

        }
    }

    // Update is called once per frame
    void Update()
    {
        Player1Movement();
        Player2Movement();
    }
}

【讨论】:

  • 哇,现在说得通了,非常感谢。我正在尝试开发自己的代码,它适用于玩家 2,但玩家 1 不能再移动了:/ 谢谢你
  • 很高兴我能提供帮助。如果您的问题得到解决,您可以接受它作为答案。 i....g....b..o 祝兄弟好运!
  • 这是 3d 比例还是 2d 比例?
  • 3D 但如果您希望它是 2D,请将所有 Vector3 替换为 Vector2。然后用 Rigidbody2D 替换 Rigidbody。
  • 当我移动我的球员时,他们会飞到空中而不是留在地面上
【解决方案2】:

为此,您可以使用Input.GetKeyDown 函数。但是,您需要为此提供一个键码才能使其工作。此函数返回一个布尔值,因此您可以在 if 语句中使用它。以下是您的 WASD 键的功能。键码看起来有点像这样。

Keycode.W

因此,例如,您的 if 语句看起来像这样if(Input.GetKeyDown(KeyCode.W)。然后移动游戏对象的变换将在给定的 x y 或 z 方向上移动,具体取决于按下的按钮。如果您愿意,可以将要移动的值乘以速度,以便稍后更改变量。如果您需要代码示例,请询问。要让每个玩家都有一个 playerid,您可以在脚本中包含一个变量,在您的 start 方法中,您可以生成一个玩家 id 或 guid 并填充该变量。如果您想在该即时脚本之外访问该变量,您需要将变量公开。

【讨论】:

  • 可以给我一个代码示例,不过我明白你的意思
猜你喜欢
  • 2020-04-24
  • 2020-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-17
  • 2015-07-10
  • 2021-01-23
相关资源
最近更新 更多