- Home /
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:
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.
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?
First, you should ask your self if using Unity's Physics system is the way to go here, as it is hard to get complete control over how things interact witch each other. You could try using physics materials that simulate a frictionless surface for the block and/or icy ground. Combine this with (or only use) one or more raycasts (more for better simulation) towards the sides of the block and downward to check if it hits a wall (set velocity to zero/stop movement) and to check if it's grounded or not. If you are using pure tile-based approach, you could even control the movement purely by script by having a way to check the terrain (or lack thereof) of each tile and use that in relation to the block's current position to make it move or not move.
Answer by DivinityStripes · Apr 19, 2016 at 05:02 AM
Problem 1: "Icy Physics":
You can apply a physics material 2D simulating zero friction to the BoxCollider2D here:
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!
Your answer
Follow this Question
Related Questions
projectiles not coliding with character 3 Answers
How do you make a 2D character jump at a fixed height no matter how much you press jump? 1 Answer
Allowing the player to draw something that turn in 2d shape with collison and solid color 1 Answer
Player falls through ground after adjusting pixels per unit of the players sprite 0 Answers