为什么我要在 Unity 游戏引擎中穿越地面?

问题描述 投票:0回答:1

我正在我的 Unity 游戏引擎中穿越地面。有谁知道我为什么要穿过地面。我正在关注教程:https://www.youtube.com/watch?v=LEUhxe9vUOM&list=PLrnPJCHvNZuCVTz6lvhR81nnaf1a-b67U&index=6&ab_channel=CodinginFlow.

这是我的 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);
        
    }
}

此外,这是我收到的错误的屏幕截图:

如果有人能帮助我,那将不胜感激。

谢谢!

c# unity3d 2d-games
1个回答
0
投票

我建议在项目设置 > Physics 2D 和游戏对象层中检查碰撞矩阵。

如果不是碰撞矩阵,也可以检查游戏对象上的碰撞器组件。在 2D 游戏中,为了让一个物体不穿过另一个物体,两个物体都应该有任何类型的 Collider2d,并且应该取消选中 isTrigger。

此外,当获得 NullReferenceException 时,这意味着脚本无法找到它需要继续的引用内容,你应该检查是否所有内容都正确添加到正确的游戏对象或被正确创建,我不确定你是否知道你的日志正在发布不是来自您的脚本,请先尝试确定来自哪里,然后看看丢失了什么。

© www.soinside.com 2019 - 2024. All rights reserved.