如何检查我的2D角色是否在Unity2D中的空中



所以我正在尝试让我的2D角色跳跃。如果我在地上跑步,效果很好,但当我撞到墙上时,它会变得很糟糕:我甚至可以在空中跳跃。我试过用bool变量来解决这个问题,但没有成功。我想检查玩家是否在空中,然后将is_on_ground变量设置为false。显然,玩家只有在地面上才能跳起来。我的代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerMovement : MonoBehaviour
{
private bool canJump = false;
public SpriteRenderer sr;
private bool facingRight;
private Animator animator;
private bool is_on_ground;
public Button leftBtn;
public Button rightBtn;
public Button jumpBtn;
public float max_velocity;
public float velocity;
public float jump_scalar;
private Rigidbody2D rb;
private float x_movement;
private Vector2 movement;
void Start() {
canJump = true;
facingRight = true;
animator = gameObject.GetComponent<Animator>();
leftBtn.onClick.AddListener(moveLeft);
rightBtn.onClick.AddListener(moveRight);
jumpBtn.onClick.AddListener(jump);
rb = gameObject.GetComponent<Rigidbody2D>();
}
private void FixedUpdate() {
if (rb.velocity.magnitude < max_velocity) {
movement = new Vector2(x_movement, 0);
rb.AddForce(movement * velocity);
}
}
private void OnCollisionEnter2D(Collision2D collision) {
if (CollisionIsWithGround(collision)) {
animator.SetBool("isJumping", false);
canJump = true;
}
if (collision.collider.tag == "wall") {
canJump = false;
}
is_on_ground = true;
}
private void OnCollisionExit2D(Collision2D collision) {
if (!CollisionIsWithGround(collision)) {
is_on_ground = false;
}
if (collision.collider.tag == "wall") {
is_on_ground = true;
canJump = true;
}
}
private bool CollisionIsWithGround(Collision2D collision) {
bool is_with_ground = false;
foreach (ContactPoint2D c in collision.contacts) {
Vector2 collision_direction_vector = c.point - rb.position;
if(collision_direction_vector.y < 0) {
is_with_ground = true;
}
}
return is_with_ground;
}
public void moveLeft() {
if (is_on_ground) {
animator.SetBool("isJumping", false);
}
animator.SetBool("isRunning", true);
sr.flipX = true;
facingRight = false;
x_movement = -1;
}
public void onRelease() {
animator.SetBool("isRunning", false);
x_movement = 0;
if(is_on_ground) {
animator.SetBool("isJumping", false);
rb.velocity = Vector3.zero;
}
}
public void moveRight() {
if (is_on_ground) {
animator.SetBool("isJumping", false);
}
if (!facingRight) {
sr.flipX = false;
facingRight = true;
animator.SetBool("isRunning", true);
} else {
animator.SetBool("isRunning", true);
}
x_movement = 1;
}
public void jump() {
if(is_on_ground) {
if (canJump) {
animator.SetBool("isJumping", true);
Vector2 jumpForce = new Vector2(0, jump_scalar * 100);
rb.AddForce(jumpForce);
}
}
}
}

问题看起来像这个

您也可以在玩家的脚周围投射一个圆圈,而不是直接向下进行光线投射。

你需要添加一个空的地面检查游戏对象作为玩家的孩子,并将其变换定位在玩家的脚上。接下来,您需要在m_WhatIsGroundLayerMask的检查器中定义哪些层算作地面。

然后在FixedUpdate()中检查地面检查变换是否在离地面的某个半径内。

// A mask determining what is ground to the character
[SerializeField] private LayerMask m_WhatIsGround;
// A position marking where to check if the player is grounded.
[SerializeField] private Transform m_GroundCheck;
// Radius of the overlap circle to determine if grounded
const float k_GroundedRadius = 0.3f;
private void FixedUpdate() {
is_on_ground = false;
// The player is grounded if a circlecast to the groundcheck position hits anything designated as ground
// This can be done using layers instead but Sample Assets will not overwrite your project settings.
Collider2D[] colliders = Physics2D.OverlapCircleAll(m_GroundCheck.position, k_GroundedRadius, m_WhatIsGround);
for (int i = 0; i < colliders.Length; i++) {
if (colliders[i].gameObject != gameObject) {
is_on_ground = true;
}
}
}

关于这种方法的教程可以在Brackey的视频中找到。

您可以在每个FixedUpdate中对地板执行Physics2d.Raycast。当距离最近物体的距离小于0.01或类似情况时,您将测试它是否为地板,如果是,则设置is_on_ground = true,如果不是,则执行is_on_ground = false

示例:

float raycastDistance = 0.05f;
RaycastHit2D hit = Physics2D.Raycast(transform.position, -Vector2.up, raycastDistance);
if (hit.collider != null && hit.transform.gameObject.layer == LayerMask.NameToLayer("ground"))
is_on_ground = true;
} else {
is_on_ground = false;
}

这应该行得通。在本例中,您显然必须定义一个名为ground的层,并将其添加到每个ground中。

最新更新