Unity 与地面碰撞不起作用(菜鸟)

发布于 2025-01-17 06:58:07 字数 1503 浏览 3 评论 0原文

因此,我刚刚开始与Unity合作,并希望我的球员只有在接触地面时才能跳跃。如果玩家用“地面”标签触摸游戏对象,我给了我的地面“地面”标签,然后检查了地面标签,以便将接地的布尔设置为真。不知何故,我的碰撞行不通。

public class PlayerMovement : MonoBehaviour
{
    [SerializeField] float speed = 10;
    private Rigidbody2D body;
    private Animator animate;
    private bool grounded;

    private void Awake()
    {
        body = GetComponent<Rigidbody2D>();
        animate = GetComponent<Animator>();
    }

    private void Update()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        body.velocity = new Vector2(horizontalInput * speed, body.velocity.y);

        if (horizontalInput > 0.01f)
            transform.localScale = new Vector3(7, 7, 7);

        else if (horizontalInput < -0.01f)
            transform.localScale = new Vector3(-7, 7, 7);

        if (Input.GetKeyDown(KeyCode.Space) && grounded == true)
            Jump();
              
            animate.SetBool("Run", horizontalInput != 0);
            animate.SetBool("grounded", grounded);
       

    }

    private void Jump()
    {
      body.velocity = new Vector2(body.velocity.x, speed);
        animate.SetTrigger("Jump");
        grounded = false;
    }

    private void OncollisionEnter2D(Collision2D collision)
    {
        Debug.Log("Collision detected");
        if (collision.gameObject.tag == "Ground")
        grounded = true;
        Debug.Log("ground is being touched");
    }
    

}

我认为让此过程的某些步骤输出日志消息是一个好主意,但是我的日志在测试后保持空。

So I just started working with unity and wanted that my player can only jump when he's touching the ground. I gave a "Ground" tag to my ground and checked, If the player is touching the gameObject with the "Ground" tag, so the grounded bool would be set to true. Somehow my collision doesnt work.

public class PlayerMovement : MonoBehaviour
{
    [SerializeField] float speed = 10;
    private Rigidbody2D body;
    private Animator animate;
    private bool grounded;

    private void Awake()
    {
        body = GetComponent<Rigidbody2D>();
        animate = GetComponent<Animator>();
    }

    private void Update()
    {
        float horizontalInput = Input.GetAxis("Horizontal");
        body.velocity = new Vector2(horizontalInput * speed, body.velocity.y);

        if (horizontalInput > 0.01f)
            transform.localScale = new Vector3(7, 7, 7);

        else if (horizontalInput < -0.01f)
            transform.localScale = new Vector3(-7, 7, 7);

        if (Input.GetKeyDown(KeyCode.Space) && grounded == true)
            Jump();
              
            animate.SetBool("Run", horizontalInput != 0);
            animate.SetBool("grounded", grounded);
       

    }

    private void Jump()
    {
      body.velocity = new Vector2(body.velocity.x, speed);
        animate.SetTrigger("Jump");
        grounded = false;
    }

    private void OncollisionEnter2D(Collision2D collision)
    {
        Debug.Log("Collision detected");
        if (collision.gameObject.tag == "Ground")
        grounded = true;
        Debug.Log("ground is being touched");
    }
    

}

I thought it would be a good idea to let certain steps of this process output a log message, but my log stayed empty after testing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

GRAY°灰色天空 2025-01-24 06:58:07

OnCollisionEnter2D 是您的正确拼写有意写。因此,将 OncollisionEnter2D 更改为 OnCollisionEnter2D,因为大小写很重要。

由于我们已经在这里并且不会造成伤害,我还建议您使用 CompareTag 而不是 tag == string 因为它的性能稍好一些,因为它更少内存分配(您可以在此处阅读:https://learn.unity.com/tutorial/fixing-performance-problems#5c7f8528edbc2a002053b595),因此习惯 CompareTag 是一个好习惯:

if(collision.gameObject.CompareTag("MyTag"))

要完成此操作答案,我想指出,您可能需要考虑修复自动完成建议(在 Visual Studio 和 Visual Studio Code: IntelliSense 中)如果它不起作用。借助此工具,您将永远不会再拼写错误,因为一旦您开始写作,就会建议正确的名称。

OnCollisionEnter2D would be the right spelling of what you intented to write. So, change OncollisionEnter2D to OnCollisionEnter2D, because capitalization matters.

As we're already here and it won't harm, I'd also suggest you to use CompareTag instead of tag == string as it performs slightly better due to less memory allocation (as you can read here: https://learn.unity.com/tutorial/fixing-performance-problems#5c7f8528edbc2a002053b595), so it's a good habit to get used to CompareTag:

if(collision.gameObject.CompareTag("MyTag"))

To finish up this answer, I'd like to point out that you might want to consider fixing your auto-completion suggestions (in Visual Studio and Visual Studio Code: IntelliSense) in case it doesn't work. With the help of this tool, you'll never misspell something like this again, because the correct name will be suggested as soon as you start writing.

回忆追雨的时光 2025-01-24 06:58:07

与你想要的不同,这个解决方案缺乏响应能力,你不能重复跳跃。 (https://www.youtube.com/watch?v=LEUhxe9vUOM )这个视频家伙使用 Boxcast。通过这种方式,boxcast方法在碰撞盒周围创建一个盒子,但它的一个参数是y偏移,在碰撞盒接触地面之前,这个盒子接触地面,这样你就可以重复跳跃。

Different from what you wanted, this solution lacks responsiveness, you cant jump repeatedly. (https://www.youtube.com/watch?v=LEUhxe9vUOM )in this video dude uses Boxcast. With this way, boxcast method creates box around the collision box, but one argument of it y offset, before collision box touches the ground, this box touches the ground thus you can jump repeadetly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文