Home > Software design >  How to register a fall from height in unity 2D
How to register a fall from height in unity 2D

Time:05-19

So i'm working on a 2D game, first stage is a platformer so i'm trying to make it look nicer. I added a particle system with dust, so in case he falls down from height animation should be played. How do i register a fall from height to play the animation?

CodePudding user response:

You can play animation on collision with the ground collider, when player's falling velocity is higher than 0. Using specific positive value may be nicer, cos it's gonna look like only achieving enough speed will spread dust.

CodePudding user response:

I can see another way of doing this, but it takes more calculations:

To define the last position and current position of Y coordinates of the player.

If it is going down currentPos < lastPos If it is going up currentPos > lastPos

public GameObject player;
public GameObject particleDust;

Vector3 lastPos;
Vector3 currentPos;
float _time = 0;

private bool up = false;
private bool down = false;
private bool equal = false;

void Awake()
{
    lastPos = player.transform.position;
}

void Update()
{
    //assign player's position to current position
    currentPos = player.transform.position;


    //going up
    if (lastPos.y < currentPos.y)
    {
        up = true;
        down = false;
        equal = false;
    }
    //going down
    else if (lastPos.y > currentPos.y)
    {
        up = false;
        down = true;
        equal = false;
    }
    //same level
    else if (lastPos.y == currentPos.y)
    {
        equal = true;
    }


    if (down == true && equal == true)
    {
        particleDust.Play();
    }


    lastPos = currentPos;

}

You can define the up and down functions to define player's movement with bool.

When the direction down is True and lastPos = currentPos then it means that he was going down and he stopped because the player hit the ground.

I choose this option without using colliders because it can be versatile and be used on every level without adding new things.

I assume this will work but it need more things, i hope this will get you started.

(If i said something wrong please correct me , it help me improve too, thank you!)

  • Related