Pushing a sliding block that falls straight down

I’m trying to copy the game mechanics of Fire n’ Ice. It’s a side scrolling, 2D puzzle game. I want to be able to push an ice block and have it slide across the floor until it hits an obstacle or falls off a ledge. If it falls of a ledge, I want it to fall straight down. I’m very new to Unity and I’m not sure how to do this.

If I give the block a RigidBody2D and a BoxCollider2D, my player can push against it. But:

  1. It slows down to a stop on the platform. I’m confused why it does this because I set the ice cube’s drag to be 0.
  2. If it falls off a ledge, it continues moving on the X axis while it’s falling. Instead, I’d like it to fall straight down.

How can I achieve physics like this?

Problem 1: “Icy Physics”:

You can apply a physics material 2D simulating zero friction to the BoxCollider2D here:
68345-physicsmaterial.png

To create the material, simply right click a folder in the project window and create a Physics Material 2D:

Name it, change the friction value to 0, and apply it to your Box Collider 2D. This should make it so the cube doesn’t “slow down to a stop”.

Problem 2: “Falling Without Horizontal Velocity”:

This problem is more difficult and could require some scripting.

While not the most robust system, to quickly make this work I would use an OnCollisionExit to tell when the cube has slid off an edge. At that moment I would set the velocity of the cube to zero so that it would just fall due to gravity without any horizontal velocity. Something like this:

using UnityEngine;
using System.Collections;

public class FallImmediately : MonoBehaviour {
    private Rigidbody2D rb;

	// Use this for initialization
	void Start () 
    {
        // Get rigidbody so we can affect its velocity
        rb = GetComponent<Rigidbody2D>();	
	}

    void OnCollisionExit(Collision col)
    {
        // Remove all velocity so that only gravity affects it
        rb.velocity = Vector2.zero;
    }
	
}

Simply attach this script to the ice block and you should be good to go.

While these are quick solutions to the problems you’re having, you should definitely consider what Cherno has to say. Some design decisions should be made at this stage to help ease any frustrations you might have with this project in the future.

Good luck!