像《涂鸦跳跃》一样的游戏。我飞越碰撞平台而不是跳跃在上面。Unity 2D

huangapple go评论71阅读模式
英文:

A game like Doodle Jump. I fly through the collider platforms instead of jumping on them. Unity 2D

问题

以下是翻译好的内容:

这些平台具有Edge Collider和Platform Effector。请提醒您,游戏是2D的。我有以下的脚本:

using UnityEngine;

public class DefaultPlatform : MonoBehaviour
{
    private void OnCollisionEnter2D(Collision2D collision)
    {
        // 从碰撞的物体获取Rigidbody2D组件
        Rigidbody2D rb = collision.collider.GetComponent<Rigidbody2D>();
        // 仅更新垂直速度,保留水平速度
        rb.velocity = new Vector2(rb.velocity.x, 8f);
    }
}

它允许您在碰到平台的碰撞体时向上跳跃,但如果这些平台在Y轴上非常接近,玩家不会选择最高的平台跳跃,而是从所有碰到的平台上跳跃。请帮帮我!我不知道如何解决这个问题。

我尝试使用协程(coroutines)等待跳跃后的几秒钟。我还尝试使用canJump和isPlayerOnPlatform变量,但没有成功。

英文:

The platforms have Edge Collider and Platform Effector. Let me remind you that the game is in 2D. I have the following script:

using UnityEngine;

public class DefaultPlatform : MonoBehaviour
{
    private void OnCollisionEnter2D(Collision2D collision)
    {
        // Get the Rigidbody2D component from the collided object
        Rigidbody2D rb = collision.collider.GetComponent&lt;Rigidbody2D&gt;();
        // Update only the vertical velocity, preserving the horizontal velocity
        rb.velocity = new Vector2(rb.velocity.x, 8f);
    }
}

It allows you to jump up when you hit a collider of platform, but if such platforms are very close to each other along the Y-axis, then the player does not choose the highest platform to jump from, but jumps from all collider of platforms that come on the way. Please help me! I have no idea how to solve this.

I tried to use coroutines to wait a few seconds after the jump. I tried to use the canJump and isPlayerOnPlatform variables, but it didn't work.

答案1

得分: 0

不需要复杂的系统,只需使用简单的 BoxCollider2D。将所有平台上的 BoxCollider2D 设置为触发器(IsTrigger 检查)。
在附加到平台游戏对象的类的 OnTriggerEnter2D 方法(wiki)中,检查玩家在Y轴上的速度(您的玩家必须具有 Rigidbody)。如果速度指向上方,则不执行任何操作,但如果速度朝下,则初始化玩家的跳跃。

英文:

You don't need complex system with Edge Collider and Platform Effector, just simple BoxCollider2D.
Make BoxCollider2D on all platforms triggers (IsTrigger check).
In OnTriggerEnter2D method (wiki) of the class, attached to the platform gameObject check player's velocity on Y-axis (your player must have Rigidbody). If it's pointing upwards, then do nothing, but if it's going down, then initialize player jump.

huangapple
  • 本文由 发表于 2023年5月31日 23:55:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375338.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定