You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity引擎中角色穿地问题技术求助

Unity角色穿地问题求助

我正在跟随CodinginFlow的2D角色移动教程开发,遇到了角色穿地的问题。以下是我的PlayerMovement脚本(PlayerMovementProject4.cs)代码:

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

public class PlayerMovementProject4 : MonoBehaviour
{
    private Rigidbody2D rb; //Will store appropriate Rigidbody2D from PlayerMovement
    private Animator anim;
    [SerializeField] private LayerMask jumpableGround;
    private float dirX = 0f;
    private SpriteRenderer sr;
    private BoxCollider2D coll;
    [SerializeField] private float moveSpeed = 7f;
    [SerializeField] private float jumpForce = 14f;

    private enum MovementState { idle, running, jumping, falling}
    

    // Start is called before the first frame update
    private void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        coll = GetComponent<BoxCollider2D>();
        sr = GetComponent<SpriteRenderer>();
        anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    private void Update()
    {
        dirX = Input.GetAxis("Horizontal"); //Might be appropriate to change to GetAxisRaw
        rb.velocity = new Vector2(dirX * 7f, rb.velocity.y);


        if (Input.GetButtonDown("Jump") && IsGrounded())
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }
        UpdateAnimationUpdate();
        
    }

    private void UpdateAnimationUpdate() //Will be used to handle Animations
    {
        MovementState myMovementState;
        
        if (dirX > 0f)
        {
            myMovementState = MovementState.running;
            sr.flipX = false;
        }
        else if (dirX < 0f)
        {
            myMovementState = MovementState.running;
            sr.flipX = true;
        }
        else //Exactly 0
        {
            myMovementState = MovementState.idle;
        }

        if (rb.velocity.y > 0.1f)
        {
            myMovementState = MovementState.jumping;
        }
        else if (rb.velocity.y < -0.1f)
        {
            myMovementState = MovementState.falling;
        }
        anim.SetInteger("state", (int)(myMovementState));
    }
    private void UpdateAnimationUpdate(float ff) //Might need to use this
    {
        dirX = ff;
        UpdateAnimationUpdate(); //DO NOT REPEAT CODE (DRY)
    }

    private bool IsGrounded()
    {
        return Physics2D.BoxCast(coll.bounds.center, coll.bounds.size, 0f, Vector2.down, .1f, jumpableGround);
        
    }
}

错误截图:
错误截图

排查与解决建议:

  • 层级与碰撞体检查:确认jumpableGround层掩码正确包含地面层级;地面的Collider2D需启用,且未勾选Is Trigger。
  • BoxCast参数优化:将IsGrounded方法中BoxCast的向下检测距离从.1f增大到.2f,避免快速下落时检测失效;可尝试缩小检测盒尺寸(如coll.bounds.size * 0.9f),减少边缘误判。
  • Rigidbody2D配置调整:把角色Rigidbody2D的Collision Detection设为Continuous,解决高速运动的碰撞穿透;检查Gravity Scale数值,过大的重力会加速下落导致穿地。
  • 代码细节修正:移动速度用定义好的moveSpeed变量替代硬编码的7f,即rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);;删除未使用的带参数UpdateAnimationUpdate方法,清理冗余代码。

内容的提问来源于stack exchange,提问作者Sid Meka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 14:27:03