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

Unity开发Stack Jump游戏:如何实现仅碰撞当前栈时玩家跳跃

Fixing Stack Jump's Collision-Only-On-Current-Stack Logic

Hey there! Let's sort out this Stack Jump collision logic issue for you. The core problem here is that your current code isn't checking which stack the player collides with—it just triggers the new stack spawn on any stack collision. Here's how to fix it step by step:

Core Concept

You need to track the current active stack (the one the player is supposed to collide with to jump) and only trigger the jump/new stack logic when the player hits that specific stack.


Step 1: Track the Current Target Stack

First, add a variable to your game manager or player script to keep track of the stack the player should interact with. For example:

  • If you're using Unity (C#):
    // In your Player or GameManager script
    private Stack currentTargetStack; // Assume you have a Stack component on each stack object
    private int currentStackIndex = 0; // Alternatively, track by index if stacks are in a list
    
  • If you're using Godot (GDScript):
    var current_target_stack: Node2D = null
    var current_stack_index: int = 0
    

Initialize this variable when the game starts—point it to the first stack in your level.


Step 2: Update Collision Detection Logic

Modify your collision handler to only respond when the player hits the currentTargetStack. Here's how to implement this in common engines:

Unity Example (2D Collision)

private void OnCollisionEnter2D(Collision2D other)
{
    // Check if the collided object is a stack
    Stack collidedStack = other.gameObject.GetComponent<Stack>();
    if (collidedStack == null) return;

    // Critical check: Is this the current target stack?
    if (collidedStack == currentTargetStack || collidedStack.Index == currentStackIndex)
    {
        // Execute your jump logic
        PerformPlayerJump();
        // Spawn the new stack
        SpawnNewStack();
        // Update the current target to the next stack (adjust based on your game's flow)
        UpdateCurrentTargetStack();
    }
    else
    {
        // Handle collision with wrong stack (e.g., game over, ignore input)
        Debug.Log("Oops! Collided with the wrong stack—game over!");
        GameOver();
    }
}

Godot Example (Body Enter Signal)

func _on_body_entered(body: Node2D):
    # Verify the collided object is a stack
    if not body.is_in_group("stacks"):
        return

    # Check if it's the current target stack
    if body == current_target_stack or body.get("stack_index") == current_stack_index:
        # Trigger jump and new stack spawn
        player_jump()
        spawn_new_stack()
        # Update target to the new stack
        update_current_target_stack()
    else:
        # Wrong stack collision handling
        print("Wrong stack hit! Game over.")
        game_over()

Step 3: Update the Current Target Stack

After spawning a new stack, make sure to update your currentTargetStack or currentStackIndex to point to the next stack the player needs to hit. For example:

// Unity example: If you store stacks in a List<Stack>
private void UpdateCurrentTargetStack()
{
    currentStackIndex++;
    currentTargetStack = stackList[currentStackIndex];
}

Key Notes

  • Tag/Group Your Stacks: Make sure all stack objects have a consistent tag (Unity) or are in a dedicated group (Godot) so you can easily identify them in collision checks.
  • Avoid Null References: If stacks are destroyed during gameplay (e.g., shrinking stacks), add checks to ensure currentTargetStack isn't null before using it.
  • Adjust to Your Game Flow: Depending on how Stack Jump works for you (e.g., stacks move horizontally, new stacks spawn above), tweak the UpdateCurrentTargetStack logic to match your specific mechanics.

The core idea stays the same across all engines: only trigger jump/new stack logic when the collision matches the actively tracked stack.

内容的提问来源于stack exchange,提问作者Avinash kumar singh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 06:57:58