如何让刚体对象“乘坐”下落的平台,同时仍然能够水平移动

问题描述

我正在 Unity 中制作一个 2D 游戏,它具有玩家可以“骑”下来的坠落平台。我希望玩家站在坠落的平台上时仍然能够向左或向右(即水平)移动。无需额外代码,仅使用 Rigidbody 物理,玩家确实会随着平台坠落,但坠落是断断续续的,并且游戏并不总是看到玩家站在平台上(很重要,因为玩家只能在站立时跳跃在某事上)。

我已尝试设置玩家对象的 transform.translate,这会阻止玩家在接触平台时向左或向右移动。我还尝试将玩家对象的 transform.parent 设置为平台的 transform,但这将玩家“粘合”到平台上,甚至防止跳跃。我也尝试过寻找解决方案/示例,并且有很多关于乘坐左右移动平台的示例,但我无法让这些示例适用于坠落的平台。

这是控制玩家左右移动和跳跃的脚本。这是附加到玩家对象:

public class Player : MonoBehavIoUr
{
  public float jumpForce = 5;
  public float horizontalSpeed = 0.1F;
  public LayerMask whatIsGround;

  private Rigidbody2D rb2d;
  private Collider2D myCollider;
  private bool grounded;
  private float lastHorizontalInput;

  void Start()
  {
    rb2d = GetComponent<Rigidbody2D>();
    myCollider = GetComponent<Collider2D>();
  }

  void Update()
  {
    // If the player is trying to jump,only do so if the player is on a platform
    grounded = Physics2D.IsTouchingLayers(myCollider,whatIsGround);
    if (Input.GetKeyDown(KeyCode.Space) && grounded)
    {
      rb2d.veLocity = new Vector2(rb2d.veLocity.x,jumpForce);
    }

    MoveHorizonltally();
  }

  private void MoveHorizonltally()
  {
    float horizontalVeLocity;
    float horizontalInput = Input.GetAxis("Horizontal");

    horizontalVeLocity = GetHorizontalVeLocity(horizontalInput);
    if (horizontalVeLocity != 0)
    {
      rb2d.position = new Vector2(rb2d.position.x + horizontalVeLocity,rb2d.position.y);
    }
  }

  private float GetHorizontalVeLocity(float horizontalInput)
  {
    // Default to no horizontal motion
    float result = 0;
    bool leftpressed = Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow);
    bool rightpressed = Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow);

    // Only a left key is being pressed; the player is going left
    if (leftpressed && !rightpressed)
    {
      result = -1 * horizontalSpeed;
    }
    // Only a right key is being pressed; the player is going right
    else if (rightpressed && !leftpressed)
    {
      result = horizontalSpeed;
    }
    // Both keys are being pressed
    else if (leftpressed && rightpressed)
    {
      // The most recent key press gets priority; ignore the oldest key press
      if (lastHorizontalInput < 0)
      {
        // Oldest key pressed is a left direction,so go right
        result = horizontalSpeed;
      }
      else
      {
        // Oldest key pressed is a right direction,so go left
        result = -1 * horizontalSpeed;
      }
    }

    lastHorizontalInput = horizontalInput;
    return result;
  }
}

这是负责使平台坠落的脚本。这是附加到平台对象:

public class PlatformFalling : MonoBehavIoUr
{
  public float fallSpeed;
  private GameObject player;

  public void Start()
  {
    player = GameObject.FindGameObjectWithTag("Player");
  }

  // Update is called once per frame
  void Update()
  {
    bool playerIsOnPlatform = player.GetComponent<Collider2D>().IsTouching(GetComponent<Collider2D>()); ;
    Vector2 falldistance = Vector2.down * fallSpeed;

    transform.Translate(falldistance);
    if (playerIsOnPlatform)
    {
      player.transform.Translate(falldistance);
    }
  }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)