【问题标题】:How to make player stop infinite jump in unity 2d如何让玩家在统一 2d 中停止无限跳跃
【发布时间】:2021-04-04 06:07:52
【问题描述】:

所以我才刚接触 Unit 2 天,我遇到了我的玩家在空中跳跃的问题。我可以继续点击空格键,我的玩家会继续跳跃而不是停止,我正在努力做到这一点,所以玩家只能在地面上而不是空中跳跃。我一直在尝试很多教程,其中大多数只是让我的游戏冻结并且不再工作。我也一直在尝试找出如何使用地面检查,但我仍然不确定如何做到这一点。

这是我知道的代码。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PM : MonoBehaviour
{
    public float moveSpeed;
    public float jumpHeight;
    Rigidbody2D rb;


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

    void Update() {
    float moveDir = Input.GetAxis("Horizontal") * moveSpeed;
    rb.velocity = new Vector2(moveDir, rb.velocity.y);

    // Your jump code:
    if (Input.GetKeyDown(KeyCode.Space))
    {
        rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
    }
   }
}

【问题讨论】:

    标签: c# unity3d 2d


    【解决方案1】:

    是的,您确实需要进行某种地面检查。网上有很多很好的教程,例如https://www.youtube.com/watch?v=c3iEl5AwUF8&t=17s(“在Unity中进行地面检查的3种方法”)。

    要实现简单的“raycast down check”,您需要为地面/平台分配一个与玩家图层不同的图层蒙版。在本例中,我添加了一个名为“Ground”的新图层。

    然后您可以使用这个简单的代码进行测试。

    BoxCollider2D boxColliderPlayer;
    int layerMaskGround;
    float heightTestPlayer;
    
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    
        // Get the player's collider so we can calculate the height of the character.
        boxColliderPlayer = GetComponent<BoxCollider2D>();
        // We do the height test from the center of the player, so we should only check
        // halft the height of the player + some extra to ignore rounding off errors.
        heightTestPlayer = boxColliderPlayer.bounds.extents.y + 0.05f;
        // We are only interested to get colliders on the ground layer. If we would
        // like to jump ontop of enemies we should add their layer too (which then of
        // course can't be on the same layer as the player).
        layerMaskGround = LayerMask.GetMask("Ground");
    }
    
    void Update()
    {
        float moveDir = Input.GetAxis("Horizontal") * moveSpeed;
        rb.velocity = new Vector2(moveDir, rb.velocity.y);
    
        // Your jump code:
        if (Input.GetKeyDown(KeyCode.Space) && IsGrounded() )
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpHeight);
        }
    }
    
    
    /// <summary>
    /// Simple check to see if our character is no the ground. 
    /// </summary>
    /// <returns>Returns <c>true</c> if the character is grounded.</returns>
    private bool IsGrounded()
    {
        // Note that we only check for colliders on the Ground layer (we don't want to hit ourself). 
        RaycastHit2D hit = Physics2D.Raycast(boxColliderPlayer.bounds.center, Vector2.down, heightTestPlayer, layerMaskGround);
        bool isGrounded = hit.collider != null;
        // It is soo easy to make misstakes so do a lot of Debug.DrawRay calls when working with colliders...
        Debug.DrawRay(boxColliderPlayer.bounds.center, Vector2.down * heightTestPlayer, isGrounded ? Color.green : Color.red, 0.5f);
        return isGrounded;
    }
    

    如你所见,我希望玩家角色拥有 BoxCollider2D - 我猜你已经拥有了,否则角色会掉到地上)。做 2D 的东西时要小心,不要混合普通的 3D 对撞机。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-26
      • 1970-01-01
      相关资源
      最近更新 更多