Dropping down trash cans

Hi everyone i just started game developing and need help. I am making 2D Roguelike game and there are trash cans in the game. I want the trash cans to fall to either left or right side when i use my dash skill but the thing is with the code i used the can is faling randomly. I want to make it so when i use dash from left to right the can is gonna fall right and when i use it from right to left the can is gonna fall to left. I gave the code below so if you can help me out i’d really appreciate it.

public class Breakable : MonoBehaviour
{
    public GameObject[] brokenPieces;


    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
       
    }

    private void OnTriggerEnter2D(Collider2D other)
    {
        if(other.tag == "Player")
        {
          if(PlayerController.instance.dashCounter > 0)
           {
             Destroy(gameObject);

             int randomPiece = Random.Range(0, brokenPieces.Length);

             Instantiate(brokenPieces[randomPiece], transform.position, transform.rotation);
           }
        }
    }
}

That code doesn’t make the can fall, it just instantiates pieces.

If your “Player”-tagged object has a Rigidbody2D component, you could get the current velocity to see which way the player is dashing.

Something like (untested):

float direction = 0f;
Rigidbody2D body = other.GetComponent<Rigidbody2D>();
if (body != null)
    direction = Mathf.Sign(body.velocity.x);
if (direction == 0f)
    direction = Mathf.Sign(UnityEngine.Random.Range(-1f, +1f));

This would give you a -1 for left, +1 for right, and a very very small chance of 0 for up/down/motionless.

Hi, i tried the code but it gave some errors, could you give me a more apparent idea about it and also is there a way we can make this 4 directions with up and down as well. I made the designs for that, only thing remaining is the code. Sorry if im being hard at understanding but like i said its just the beginnig for me so i thought it would be best to ask the community about this.

How are you dashing? Are you using physics properly?

With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.

Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things. Doing this keeps the physics system informed about what is going on.

nah its not that deep, i just need the image of fallen trash can to be seen left or right correctly only thing i need is the code because its not a physics problem, just gotta get some code so that it makes the falling image correctly. Like i said im new and not very good at writing new codes, i usually look at some youtube videos and stuff but this time i couldnt fina a solution on google.

I’m not sure what you mean by this… but… at the end of the day it will be a structure like:

if (<condition here for facing right>)
{
  // code here to make it face right
}
if (<condition here for facing left>)
{
  // code here to make it face right
}